Last active
August 17, 2017 09:49
-
-
Save A1rPun/2fadcd4578d63bc7b0500eced701186a to your computer and use it in GitHub Desktop.
Waterfall on promise
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
waterfall(['a', 'bbbb', 'cc'], function(item){ | |
return function(resolve){ | |
setTimeout(function(){ | |
console.log(item); | |
resolve(); | |
}, item.length * 1000); | |
}; | |
}); |
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
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