Created
August 7, 2020 18:17
-
-
Save darnfish/6d454b54e9b646da41ec9740ea21eb56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(promises, { seq, progress } = {}) { | |
const results = new Array(promises.length).fill(undefined) | |
let count = 0 | |
return new Promise(async (resolve, reject) => { | |
async function runPromise(promise, i) { | |
results[i] = await promise | |
count += 1 | |
if(progress) | |
progress((count / promises.length).toFixed(2)) | |
if(count === promises.length) | |
resolve(results) | |
} | |
if(seq) | |
for(let i = 0; i < promises.length; i++) | |
await runPromise(promises[i], i) | |
else | |
promises.forEach(runPromise) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment