Skip to content

Instantly share code, notes, and snippets.

@damienklinnert
Created February 19, 2013 08:37
Show Gist options
  • Save damienklinnert/4984077 to your computer and use it in GitHub Desktop.
Save damienklinnert/4984077 to your computer and use it in GitHub Desktop.
What if we'd like to set off a bunch of async calls at once, and wait until they all finish (at various times, in any order) to set off the next promise in the chain? Q provides a simple method that takes an array of promises: Q.all()
function get_all_the_things(things) {
var the_promises = [];
for (var i = 0; i < things.length; i++) {
(function(i) { // keep i in scope
var deferred = Q.defer();
get_a_thing(things[i], function(result) {
deferred.resolve(result);
});
the_promises.push(deferred.promise);
})(i);
}
return Q.all(the_promises);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment