The only thing I don’t like is that this brings us back to callbacks, which so elegantly got rid of with await.
One way I was able to deal with 'callback hell':
(previous) => next
so now I can:
await getSomeRecordsAsync() // => Promise<Result<SomeRecords, SomeError>>
map(someRecords => someRecords.map(x => x.someField).join(", ")), // => Promise<Result<string, SomeError>>
mapErr(err => err.map(x => x.message).join(", ")), // => Promise<Result<string, string>>
flatMap(saveStringAsync), // => Promise<Result<void, someError>>
(with full Typescript typing support btw!)
Thinking about releasing it to GitHub and npm at some point :)
Would be great if typescript allows pattern matching, then you’ll do
Err(e) => console.log(“error!”);
Ok(value) => console.log(value);
Maybe one day!
Actually you can do something similar:
It’s not as pretty as real pattern matching but it fits to this use car.
My post, framework and sample just landed dev.to/patroza/result-composition-...
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.