Created
February 19, 2013 08:37
-
-
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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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