type Handler = () => T | Promise; type Checker = (result: T) => T | Promise; export class Fallback { private handlers: Handler[] = []; private checker: Checker; constructor(checker?: Checker) { this.checker = checker || (async (result: T) => result); } add(handler: Handler): this { this.handlers.push(handler); return this; } // 执行处理程序链 async run(): Promise { const errors: Error[] = []; for (const handler of this.handlers) { try { const result = await handler(); let data = await this.checker(result); if (data) { return data; } } catch (error) { console.log(error); errors.push(error instanceof Error ? error : new Error(String(error))); } } throw new AggregateError(errors, 'All handlers failed'); } } export class FallbackUtil { static boolchecker(value: T, condition: boolean): T { if (condition) { return value; } else { throw new Error('Condition is false, throwing error'); } } }