Skip to content

Instantly share code, notes, and snippets.

@marsgpl
Last active April 28, 2024 22:48
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 marsgpl/3fbb6267a18eaeab0afd84d220e5759f to your computer and use it in GitHub Desktop.
Save marsgpl/3fbb6267a18eaeab0afd84d220e5759f to your computer and use it in GitHub Desktop.
promiseAll.ts
async function promiseAll<T = unknown>(promises: Promise<T>[]): Promise<T[]> {
const results: T[] = []
for (let i = 0; i < promises.length; ++i) {
results.push(await promises[i])
}
return results
}
function promiseAll2<T = unknown>(promises: Promise<T>[]): Promise<T[]> {
return new Promise((resolve, reject) => {
const results: T[] = []
promises.forEach(promise => promise.then(result => {
results.push(result)
if (results.length === promises.length) {
resolve(results)
}
}).catch(reject))
resolve(results)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment