Created
January 10, 2020 02:34
-
-
Save girvan/b0db540d69b3edbf591b874045e521ff to your computer and use it in GitHub Desktop.
run promise / task in sequence
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
var tasks = [ | |
function(resolve) { | |
return setTimeout(function() { | |
console.log(1); | |
resolve(); | |
}, 300); | |
}, | |
function(resolve) { | |
return setTimeout(function() { | |
console.log(2); | |
resolve(); | |
}, 200); | |
}, | |
function(resolve) { | |
return setTimeout(function() { | |
console.log(3); | |
resolve(); | |
}, 100); | |
}, | |
]; | |
tasks.reduce(function(promise, task) { | |
return promise.then(function() { | |
return new Promise(function(res) { | |
task(res); | |
}); | |
}); | |
}, Promise.resolve()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment