Reputation: 1062
promise.each(function(obj){
obj.doIntensiveWork()
.then(function(){
console.log("I AM FIRST")
})
})
.then(function(){
console.log("I AM SECOND")
})
let's say doIntensiveWork()
takes quite some time to fulfill. "I AM SECOND" would be logged before "I AM FIRST", since each
is resolved as doIntensiveWork()
is called (but not fulfilled) on every obj. Is that correct?
If yes, how can I invert this to make sure "I AM SECOND" gets logged only once and after the last "I AM FIRST" log? I am using the Bluebird API
Upvotes: 1
Views: 187
Reputation: 22442
You are correct in that each does not wait for the intensiveWork to be finished. You can 'join' all the intensive work by using the map
function (do not forget the return
)
promise.map(function(obj){
return obj.doIntensiveWork()
.then(function(){
console.log("I AM FIRST")
})
})
.then(function(){
console.log("I AM SECOND")
})
Upvotes: 3