Created
March 22, 2018 02:24
-
-
Save YiqinZhao/c6a98d1c6e02eafea9e8a903fa5c8b53 to your computer and use it in GitHub Desktop.
[Promise waterfall] Async function queue #interview
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 func1 = function() { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('func1'); | |
resolve(); | |
}, 500); | |
}); | |
}; | |
var func2 = function() { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('func2'); | |
resolve(); | |
}, 300); | |
}); | |
}; | |
var func3 = function() { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('func3'); | |
resolve(); | |
}, 100); | |
}); | |
}; | |
var asyn = function(arr, cb) { | |
arr.reduce((p, func) => p.then(func), Promise.resolve()) | |
.then(cb); | |
}; | |
asyn([func1, func2, func3], () => { console.log('Finished!') }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment