I swear by Q for its simplicity, so I might not have done much research checking out other 'then' implementations. But I've used Q a good deal!
I have a chain of 'then' promises, and I want to resolve a 'batch' of promises in the middle of it and proceed with other operations in sequence; so it's obvious I should use Q.all. But I'm stuck here. Either Q's doing it wrong, or I am.
Here are two hypothetical async operations
var f=function(delay){
return Q.delay(delay).then(function(){
console.log("returning delayed",delay)
return delay
})
}
f2=function(delay){
var defer=Q.defer()
setTimeout(function(){
console.log("returning timedout",delay)
return delay
},delay)
return defer.promise
}
And this is the promise chain
Q('begin')
.then(console.log)
.then(Q.all([100,200,300,400].map(f)))
.then(function(){
console.log("Finally",arguments)
}).done()
And this is the output I wish
begin
returning delayed 100
returning delayed 200
returning delayed 300
returning delayed 400
Finally { '0': undefined }
But this is the output I get
begin
Finally { '0': undefined }
returning delayed 100
returning delayed 200
returning delayed 300
returning delayed 400
I get the same sequence with f2
Now, if I run this
Q.all([100,200,300,400].map(f)).then(function(){
console.log("Finally",arguments)
}).done()
I do get
returning delayed 100
returning delayed 200
returning delayed 300
returning delayed 400
Finally { '0': [ 100, 200, 300, 400 ] }
but using f2
instead of f
gives me
returning timedout 100
returning timedout 200
returning timedout 300
returning timedout 400
It doesn't execute the finally
block.
I get the same outputs with Q.[all/allResolved/allSettled]
So, my queries are,
- How do I achieve the intended output by using Q.all specifically. I do have a workaround though, but it doesn't look nice.
- How are
f
andf2
different since the results by running Q.all().then() with them aren't the same.