/** * This function emulates the Promise.allSettled() * * @param proms - */ export function allSettled( proms: Promise[] ): Promise<({ value: any; status: string } | { reason: Error; status: string })[]> { return Promise.all(proms.map(reflect)); } export function reflect(prom: Promise) { return prom .then((value: T) => ({ value, status: 'fulfilled' })) .catch((reason: T) => ({ reason, status: 'rejected' })); } /** * This function emulates the Promise.any() * * @param values - */ export function promiseAny(values: Iterable>): Promise { return new Promise((resolve: (value: T) => void, reject: (reason?: any) => void): void => { let hasResolved: boolean = false; const promiseLikes: (T | PromiseLike)[] = []; let iterableCount: number = 0; const rejectionReasons: any[] = []; function resolveOnce(value: T): void { if (!hasResolved) { hasResolved = true; resolve(value); } } function rejectionCheck(reason?: any): void { rejectionReasons.push(reason); if (rejectionReasons.length >= iterableCount) reject(rejectionReasons); } for (const value of values) { iterableCount++; promiseLikes.push(value); } for (const promiseLike of promiseLikes) { if ((promiseLike as PromiseLike)?.then !== undefined || (promiseLike as Promise)?.catch !== undefined) { (promiseLike as Promise)?.then((result: T): void => resolveOnce(result))?.catch((): void => undefined); (promiseLike as Promise)?.catch((reason?: any): void => rejectionCheck(reason)); } else resolveOnce(promiseLike as T); } }); }