Created
December 7, 2017 07:43
-
-
Save lslinnet/1942d6b3df73a9bbcad1ba757a6fd591 to your computer and use it in GitHub Desktop.
Process all requests guaranteeing synchronous execution (e.g. 1 then 2 then 3)
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
function asyncFunction (item, cb) { | |
setTimeout(() => { | |
console.log('done with', item); | |
cb(); | |
}, 100); | |
} | |
let requests = [1, 2, 3].reduce((promiseChain, item) => { | |
return promiseChain.then(() => new Promise((resolve) => { | |
asyncFunction(item, resolve); | |
})); | |
}, Promise.resolve()); | |
requests.then(() => console.log('done')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment