Skip to content

Instantly share code, notes, and snippets.

@A1rPun
Last active August 17, 2017 09:49
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 A1rPun/2fadcd4578d63bc7b0500eced701186a to your computer and use it in GitHub Desktop.
Save A1rPun/2fadcd4578d63bc7b0500eced701186a to your computer and use it in GitHub Desktop.
Waterfall on promise
waterfall(['a', 'bbbb', 'cc'], function(item){
return function(resolve){
setTimeout(function(){
console.log(item);
resolve();
}, item.length * 1000);
};
});
function waterfall(items, resolver) {
items.reduce(function (promise, item) {
return promise.then(function () {
return new Promise(resolver(item));
});
}, Promise.resolve());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment