1

In RSVP.js, there is a very elegant idiom:

var promises = [2, 3, 5, 7, 11, 13].map(function(id){
  return getJSON("/post/" + id + ".json");
});

RSVP.all(promises).then(function(posts) {
  // posts contains an array of results for the given promises
}).catch(function(reason){
  // if any of the promises fails.
});

However I am using a library that already relies on, and exposes some of bluebird's api. So I'd rather avoid mixing in RSVP.js even if it may seem at times more elegant.

What would be the equivalent in bluebird, of the RSVP.js code snippet above?

matanster
  • 15,072
  • 19
  • 88
  • 167

1 Answers1

3

Except for using Bluebird's Promise namespace instead of RSVP, everything can stay the same - using Promise.all. Also, mixing promises that conform to the Promises A+ specification should work well, so you might not even have to change anything.

While I personally don't like it much, Bluebird also has its own idiom for this task - Promise.map:

Promise.map([2, 3, 5, 7, 11, 13], function(id){
  return getJSON("/post/" + id + ".json");
}).then(function(posts) {
  // posts contains an array of results for the given promises
}).catch(function(reason){
  // if any of the promises fails.
});
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks guys, my real code that resembles the above works now, using `Promise.all` (and a regular non-Promises map) indeed. I just got lost before with the detail of the `bluebird` example for map (where it promisifies an existing library and stuff in addition to the `.all`. Thanks :) – matanster Aug 18 '14 at 18:50