Instantly share code, notes, and snippets.

Embed
What would you like to do?
Promise.while
var Promise = require("bluebird");
Promise.while = function(condition, action) {
var resolver = Promise.defer();
var loop = function() {
if (!condition()) return resolver.resolve();
return Promise.cast(action())
.then(loop)
.catch(resolver.reject);
};
process.nextTick(loop);
return resolver.promise;
};
var i = 0;
Promise.while(function() {
return i < 10;
}, function() {
return new Promise(function(resolve, reject) {
i++;
if (i == 10) response = 'it is done!';
resolve(response);
})
})
.then(function(response) {
assert(response == 'it is done!');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment