Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tap on a rejected promise triggers unhandled promise rejection #74

Closed
TheSpyder opened this issue Mar 24, 2021 · 3 comments
Closed

tap on a rejected promise triggers unhandled promise rejection #74

TheSpyder opened this issue Mar 24, 2021 · 3 comments

Comments

@TheSpyder
Copy link

Replication code:

open Promise.Js;
rejected("my error")
->tap(Js.log2("debug print", _))
->catch(e => {
    Js.log2("regular error handling:", e);
    resolved();
  })
->get(_ => ());

This prints regular error handling but also triggers an unhandled promise rejection.

The cause, as far as I can tell, is that tap forks the promise and moves the callback out of the error handling chain:
https://github.com/aantron/promise/blob/master/src/js/promise.re#L122-L125

I think the implementation should use map directly, instead of get, executing the callback and returning the same value. This keeps the tap side effect in the same chain as normal error handling.

  let tap = (promise, callback) =>
    map(promise, (v) => {
      callback();
      v
    });
@aantron aantron closed this as completed in 356dd59 May 2, 2021
@aantron
Copy link
Owner

aantron commented May 2, 2021

@TheSpyder Thanks for the report and the suggestion. I've added a reproducing test case in 356dd59, and your fix. It's now in npm as reason-promise 1.1.4. Apologies for the pretty big delay.

@aantron
Copy link
Owner

aantron commented May 2, 2021

I updated only Promise.Js.tap, but not Promise.tap, Promise.tapOk, Promise.tapError, or Promise.tapSome. In an idealized type-safe world, these functions would never face promise rejections, and it might actually be good for them to cause rejection reports, if they do. Please let me know if you have any issues with that, and would like them changed, as well.

@TheSpyder
Copy link
Author

Thanks! I was tempted to log a PR but I wasn't sure if I had the right idea :)

I think it's fine for this to only work with Promise.Js. Those are the only ones I feel the need to tap anyway, since I use them for bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants