Skip to content

Instantly share code, notes, and snippets.

@YiqinZhao
Created March 22, 2018 02:24
Show Gist options
  • Save YiqinZhao/c6a98d1c6e02eafea9e8a903fa5c8b53 to your computer and use it in GitHub Desktop.
Save YiqinZhao/c6a98d1c6e02eafea9e8a903fa5c8b53 to your computer and use it in GitHub Desktop.
[Promise waterfall] Async function queue #interview
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