Skip to content

Instantly share code, notes, and snippets.

@rektide
Created July 29, 2012 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rektide/3202038 to your computer and use it in GitHub Desktop.
Save rektide/3202038 to your computer and use it in GitHub Desktop.
Q's spread
var Q= require("q")
// show time going by
var n= 0
function log(){console.log("..."+(++n))}
// we have to deferreds
var a= Q.defer(),
b= Q.defer()
Q.spread([a.promise,b.promise],function(a_,b_){
console.log("done-via-spread",a_,b_)
})
// Expected to work:
// "And you can use Q.spread directly on an array of promises."
// but calls back immediately, before resolves happen
Q.all([a.promise,b.promise]).spread(function(a_,b_){
console.log("done-via-all-spread",a_,b_)
})
// Works suitably.
// Is there a shorter way to do this?
// schedule resolves:
process.nextTick(function(){
log()
a.resolve("yes")
log()
})
process.nextTick(function(){
log()
b.resolve("yes")
log()
})
@kriskowal
Copy link

Q should make Q.spread implicitly Q.all the array of arguments. It doesn’t presently. I’ll file an issue if you don’t.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment