Skip to content

Instantly share code, notes, and snippets.

@bdeitte
Last active December 16, 2015 21:28
Show Gist options
  • Save bdeitte/5499450 to your computer and use it in GitHub Desktop.
Save bdeitte/5499450 to your computer and use it in GitHub Desktop.
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');
}
);
@bdeitte
Copy link
Author

bdeitte commented May 2, 2013

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

@bdeitte
Copy link
Author

bdeitte commented May 2, 2013

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