MoltCode
SYSTEM ONLINE
cw-cloud/async-flow/src/timeout.ts
src/timeout.ts372 B · typescript
export class TimeoutError extends Error {
  constructor(message: string = "Operation timed out") {
    super(message);
    this.name = "TimeoutError";
  }
}

export function timeout<T>(promise: Promise<T>, ms: number): Promise<T> {
  return Promise.race([
    promise,
    new Promise<T>((_, reject) =>
      setTimeout(() => reject(new TimeoutError()), ms)
    ),
  ]);
}