Bluebird: Promise.map return only the first rejected promise if multiple promises rejects

Created on 23 Jan 2017  路  4Comments  路  Source: petkaantonov/bluebird

1) What version of bluebird is the issue happening on?
version 3.4.6

2) What platform and version? (For example Node.js 0.12 or Google Chrome 32)
Node.js 6.9.1
3) Did this issue happen with earlier version of bluebird?
Don't know

Hello,
maybe I am doing something wrong, but I noticed that Promise.map returns an array of Promise resolutions in case all the promises are resolved correctly, but instead it return the reject object of the FIRST rejected promises even if the rejected promises are more than one.

Example:
Let's say we have an array of promises var promises = [Promise1, Promise2, Promise3, Promise4], and each promises return "OK" if the promise resolve, and "FAIL" if the promise rejects.

If i call

Promise.map(promises, (promise) => {
        // do stuff
})
.then((res) => {
     console.log(res);
})
.catch((err) => {
     console.log(err);
})

I can see that, if all the promises resolve correctly, res is an array of promises resolved data
res = ["OK", "OK", "OK", "OK"]

On the other hand, if one or more of the promises reject, the Promise.map goes to .catch() and I get only the catch data of the first promise that rejects.
So if the promises Promise2 and Promise3 reject, it goes to .catch, with err = {"FAIL"} instead, for example, an array with all the responses err = ["OK", "FAIL", "FAIL", "OK"].

So basically I can only know which is the first rejected promise, and I don't know which promises actually resolved.

Am I missing something? Am I using the wrong API?
I need this behaviour because in case of rejected promise, I need to notify the caller telling which promise succeeded and which failed.

Thanks in advance for the help.

All 4 comments

@spion thanks for the answer.

That is exactly the workaround I used to accomplish my task but I think it isn't a clean solution.

Furthermore I just want to tell that the Promise.map method would be more useful if implemented as I described previously.

An API should be built of composable parts - reflect with map do what you desire and .map is already set in stone as we won't break users' code for it anyway.

@benjamingr I'm not talking about change the Promise.map implementation, but maybe provide a new method to accomplish it.
My comment is more like a feature request and not a critic. I see other people request a feature like this.

Thanks for the answer.
Simone

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jefflewis picture jefflewis  路  6Comments

wearhere picture wearhere  路  5Comments

julien-f picture julien-f  路  5Comments

icodeforlove picture icodeforlove  路  3Comments

rohitbegani picture rohitbegani  路  6Comments