import _ from "lodash"; import { getNS, stringifyError } from "../common"; import { Executor } from "./Executor"; import { FnBox } from "./FnBox"; export class RaceExecutor extends Executor { constructor(private readonly shouldFailWhenFastestFails: boolean = false) { super(); } override async execute(functions: FnBox[]): Promise { const date = Date.now(); const indexedPromises = Executor.getPromises(functions).map((promise, index) => promise.then((result) => ({ index, result })) ); const winner = await (this.shouldFailWhenFastestFails ? Promise.race(indexedPromises) : Promise.any(indexedPromises)); this.logger.info(`Promise #${winner.index} won in ${Date.now() - date} [ms]`); void Promise.allSettled(indexedPromises).then((results) => { const failed = results .map((result, index) => ({ ...result, index })) .filter((r) => r.status === "rejected"); if (failed.length > 0) { this.logger.warn( `Race completed successfully, but ${getNS(failed.length, "other promise")} failed:`, _.map( failed, (item) => `Promise #${item.index} failed: ${stringifyError(item.reason, true)}` ) ); } }); return winner.result; } }