Skip to content

Instantly share code, notes, and snippets.

@aponxi
Forked from victorquinn/promise_while_loop.js
Created June 17, 2018 02:58
Show Gist options
  • Select an option

  • Save aponxi/f8207b30d301ce4722788952de2ed593 to your computer and use it in GitHub Desktop.

Select an option

Save aponxi/f8207b30d301ce4722788952de2ed593 to your computer and use it in GitHub Desktop.
Promise "loop" using the Bluebird library
var Promise = require('bluebird');
var promiseWhile = function(condition, body) {
var resolver = Promise.defer();
var loop = function() {
if (!condition()) return resolver.resolve();
return Promise.cast(body())
.then(loop)
.catch(resolver.reject);
};
process.nextTick(loop);
return resolver.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment