Skip to content

Instantly share code, notes, and snippets.

@JaminFarr
Last active April 27, 2016 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaminFarr/14a2e6f5d253cd3cdce2b35a3834086a to your computer and use it in GitHub Desktop.
Save JaminFarr/14a2e6f5d253cd3cdce2b35a3834086a to your computer and use it in GitHub Desktop.
promiseWhile
// Based on http://blog.victorquinn.com/javascript-promise-while-loop and https://github.com/stevenzeiler/promise-while
function promiseWhile(condition, action) {
return new Promise((resolve, reject) => {
const loop = () =>
Promise.resolve(condition())
.then(keepGoing => {
if (!keepGoing) { return resolve(); }
Promise
.resolve(action())
.then(loop);
})
.catch(err => reject(err));
loop();
});
}
var promiseNot = condition => Promise.resolve(condition).then(result => !result);
var promiseUntil = (condition, action) => promiseWhile(() => promiseNot(condition()), action);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment