Skip to content

Instantly share code, notes, and snippets.

@darnfish
Created August 7, 2020 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darnfish/6d454b54e9b646da41ec9740ea21eb56 to your computer and use it in GitHub Desktop.
Save darnfish/6d454b54e9b646da41ec9740ea21eb56 to your computer and use it in GitHub Desktop.
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