Last active
December 16, 2015 21:28
-
-
Save bdeitte/5499450 to your computer and use it in GitHub Desktop.
This file contains 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'); | |
var allPromises = function (arrayOfThings) { | |
var promises = []; | |
arrayOfThings.forEach(function(aThing) { | |
console.log('updating a thing: ' + aThing); | |
var promise = Q.fcall(function () { | |
console.log("in func promise"); | |
return 'something'; | |
}) | |
.delay(1000) | |
.done(function() { | |
console.log('done with func promise'); | |
}); | |
promises.push(promise); | |
}); | |
console.log('returning a promise'); | |
return Q.allResolved(promises); | |
}; | |
allPromises(['a', 'b', 'c']) | |
.done(function() { | |
console.log('done with everything'); | |
} | |
); |
Switching "done" to "then" in the function is the answer, so that the "promise" variable actually has a promise.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would expect 'done with everything' to show up last. But no matter how rework allPromises, I always see it have the potential of going last. (The delay is in allPromises to make sure that it can be seen that allPromises happens last.) Here's the output from the above:
updating a thing: a
updating a thing: b
updating a thing: c
returning a promise
in func promise
in func promise
in func promise
done with everything
done with func promise
done with func promise
done with func promise