Skip to content

Instantly share code, notes, and snippets.

@Arnavion
Arnavion / async-await.ts
Last active May 30, 2021 06:24
Promise.first
async function first<T>(promises: Promise<T>[]): Promise<T> {
const rejections: any[] = [];
for (const promise of promises) {
try {
return await promise;
}
catch (reason) {
rejections.push(reason);
}
@Arnavion
Arnavion / async-await.ts
Last active May 30, 2021 06:25
Promise.any - async/await vs then()
async function any<T>(promises: Promise<T>[]): Promise<T> {
const rejections: any[] = [];
for (const promise of promises) {
try {
return await promise;
}
catch (reason) {
rejections.push(reason);
}