Created
July 29, 2012 21:48
-
-
Save rektide/3202038 to your computer and use it in GitHub Desktop.
Q's spread
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
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() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.