Please answer the questions the best you can:
1) What version of bluebird is the issue happening on?
3.5.1
2) What platform and version? (For example Node.js 0.12 or Google Chrome 32)
Node 6.12.2, Mac OS 10.12.6
3) Did this issue happen with earlier version of bluebird?
No. Issue not present in bluebird 3.5.0.
Chaining .map() or .filter() on to a rejected promise leads to an unhandled rejection, even when .catch() handler is attached further down the chain.
Promise.reject( new Error('foo') )
.map( () => {} )
.catch( () => {} );
With bluebird v3.5.1, the above produces Unhandled rejection Error: foo. In v3.5.0 no unhandled rejection is created.
I assume is caused by https://github.com/petkaantonov/bluebird/commit/48c8591c0ad3fc6a8f6dc38956fe6a08dd0b617f but I can't understand why!
Don't know if this is related to https://github.com/petkaantonov/bluebird/issues/1468.
Promise.map() and Promise.filter() show the same problem.
This produces an unhandled rejection:
const p = Promise.reject( new Error('foo') );
Promise.map(p, () => {} )
.catch( () => {} );
Having looked into this further, I think this problem was actually introduced a while back in a PR that I submitted: https://github.com/petkaantonov/bluebird/pull/1221
That PR introduced a call to async.invoke() immediately on calling Promise.map(). So no handlers are added to the input promise in that tick. Hence if the input promise is rejected that it is indeed an unhandled rejection.
I guess what should be happening is that the input promise should be checked for if it's already rejected and if it is the promise returned by Promise.map() immediately rejected. And only after that, iterating over the array should be delayed by async.invoke().
Actually, the tick should be introduced after the first round of iteration, but before the map handers are called. Otherwise there can also be unhandled exceptions on any rejected promises in the input array.
At present, the following also creates an unhandled exception:
const arr = [ Promise.reject( new Error('foo') ) ];
Promise.map( arr, () => {} ).catch( () => {} );
Please see PR #1489 which fixes this.
I've experienced this issue as well. What is the status of this issue? the fix is planned to be merged soon?
Most helpful comment
I've experienced this issue as well. What is the status of this issue? the fix is planned to be merged soon?