typescript-functional-extensions icon indicating copy to clipboard operation
typescript-functional-extensions copied to clipboard

.mapError doesnt work with async

Open therepo90 opened this issue 1 year ago • 2 comments

ResultAsync.from(yourpromise).mapError(e => {}) map error is not being invoked when promise is throwing.

therepo90 avatar Jul 31 '24 13:07 therepo90

Yes, currently ResultAsync.from() does not convert a rejected Promise into a failed Result - the Promise is expected to resolve to a Result.

The JS Doc comment states this:

  /**
   * Creates a new ResultAsync from the given Promise
   * @param value a Promise resolving to a Result
   */

So, at the moment it is up to you to ensure your Promise handles the failure with a .catch(e => Result.failure(...))

Maybe I could make a separate ResultAsync.fromPromise() that has the functionality you are looking for?

seangwright avatar Jul 31 '24 15:07 seangwright

Thanks for the answer. IDK, its counter-intuitive to do result async from a promise and have api .maperror that doesnt catch errors from this promise, also its not mentioned in the docs how to use that. So basically u have to map firslty the promise to result fialure and then map error, its not something you come up with. Actually i just stopped using monads here, just doing the .catch() explicitly without Result chain. Just mention it in the docs and youll be fine

therepo90 avatar Aug 02 '24 09:08 therepo90