Skip to content

Instantly share code, notes, and snippets.

@benweizhu
Last active May 11, 2018 03:18
Show Gist options
  • Save benweizhu/6f326ce18bc9a043b40d59bca1c5bdf4 to your computer and use it in GitHub Desktop.
Save benweizhu/6f326ce18bc9a043b40d59bca1c5bdf4 to your computer and use it in GitHub Desktop.
promises reduce to promise chain
const array = [1, 2, 3, 4, 5];
array
.reduce(function(promise, currentValue, currentIndex) {
return promise
.then(function() {
return new Promise(function(resovle) {
setTimeout(function() {
console.log("send", currentValue);
resovle(currentValue);
}, 1000);
});
})
.then(function(currentValue) {
return new Promise(function(resolve) {
setTimeout(function() {
console.log("wait", currentValue);
resolve(currentValue);
}, 1000);
});
});
}, Promise.resolve())
.then(function(data) {
console.log(data)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment