Last active
May 11, 2018 03:18
-
-
Save benweizhu/6f326ce18bc9a043b40d59bca1c5bdf4 to your computer and use it in GitHub Desktop.
promises reduce to promise chain
This file contains hidden or 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
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