Last active
April 27, 2016 13:30
-
-
Save JaminFarr/14a2e6f5d253cd3cdce2b35a3834086a to your computer and use it in GitHub Desktop.
promiseWhile
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
// 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