Skip to content

Instantly share code, notes, and snippets.

@lslinnet
Created December 7, 2017 07:43
Show Gist options
  • Save lslinnet/1942d6b3df73a9bbcad1ba757a6fd591 to your computer and use it in GitHub Desktop.
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)
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