Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Last active June 9, 2020 18:17
Show Gist options
  • Save throughnothing/41a797471b29ee25000ca2bee07578ed to your computer and use it in GitHub Desktop.
Save throughnothing/41a797471b29ee25000ca2bee07578ed to your computer and use it in GitHub Desktop.
Promise.all implementations ... for fun
// Reduce Implementation
function promiseAllReduction(promises) {
return promises.reduce(function(acc, i) {
return acc.then(rs => i.then(r => [...rs, r]))
}, Promise.resolve([]));
}
// Recursive Implementation
function promiseAllRecursive(promises) {
if(promises.length === 0) {
return Promise.resolve([]);
}
const [p, ...ps] = promises;
return p.then(r => {
return PromiseAllRecursive(ps).then(rs => [r, ...rs])
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment