Last active
June 9, 2020 18:17
-
-
Save throughnothing/41a797471b29ee25000ca2bee07578ed to your computer and use it in GitHub Desktop.
Promise.all implementations ... for fun
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
// 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