Last active
April 28, 2024 22:48
-
-
Save marsgpl/3fbb6267a18eaeab0afd84d220e5759f to your computer and use it in GitHub Desktop.
promiseAll.ts
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
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