Alex W.'s Blog

Tasks and Promises in fp-ts

In fp-ts a Task is just a a lazy Promise, eg. () => Promise, since Promises are eager normally (ie. they execute their computation when created, not when await-ed on). See getLine in fp-ts-contrib for an example.

A TaskEither is for representing Promises that can reject. The reason the tryCatch method, intended for converting a Promise to a TaskEither requires a second function for handling errors is because a Promise doesn’t store type information about the error. This is in contrast to callback-style async functions which include the error type as the first parameter of the callback, and which is why taskify exists for directly turning them into functions returning TaskEither.

It’s unclear to me why there isn’t a tryCatch variant that assumes the identity function for the second parameter, thus converting a Promise<A> into a TaskEither<unknown, A>.

Written by Alex Wendland, who is currently exploring fintech at Pinwheel. Here's a Twitter handle, a GitHub, some LinkedIn, ol' Facebook, or a hip Instagram if you're interested.