The promise of iteration completely distorted my brain the first time I tried it. I think that the Bluebird documentation does a rather poor job than the usual use cases, but I will not continue it because (a) I love Bluebird, and (b) I do not have time to update the documents.
It seems to me that Promise.map is the right thing for your scenario.
myService.getSomeData(url) .then((data) => { return myOtherService.getMoreData(data.uniqueId) }) .map((item) => { return doSomethingWithData(item); }) .then((results) => { // do something with the result array. });
Depending on what you want to do with the results where I used .map , you can also use .reduce or .each . Please note that .each does not change the return value from the promise to which it is attached, so the βuse only for side effectsβ comment in Bluebird docs.
The difference between instance and static methods, of course, is that with statics you have to provide an array, for example. Promise.map(array, (item) => {}) .
Also, as @jib said, always return a value inside your callbacks. It will save you from pain.
Paul Midgen May 7 '16 at 11:36 a.m. 2016-05-07 11:36
source share