Skip to content

Instantly share code, notes, and snippets.

@disco0
Forked from tyler-johnson/asyncwhile.js
Created October 4, 2020 08:08
Show Gist options
  • Save disco0/e2dc4a7ea26c7fc5eba347a16d8798ee to your computer and use it in GitHub Desktop.
Save disco0/e2dc4a7ea26c7fc5eba347a16d8798ee to your computer and use it in GitHub Desktop.
Asynchronous while loop for ES6 Promises.
function asyncWhile(condition, action, ctx) {
var whilst = function(data) {
return condition.call(ctx, data) ?
Promise.resolve(action.call(ctx, data)).then(whilst) :
data;
}
return whilst();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment