/** * Timeout utilities for operations */ /** * Create a timeout promise that rejects after specified time */ export function createTimeout(ms: number, message = 'Operation timed out'): Promise { return new Promise((_, reject) => { setTimeout(() => reject(new Error(message)), ms); }); } /** * Race a promise against a timeout */ export async function withTimeout( promise: Promise, timeoutMs: number, timeoutMessage = 'Operation timed out' ): Promise { return Promise.race([ promise, createTimeout(timeoutMs, timeoutMessage), ]); } /** * Create a timeout wrapper function */ export function timeout Promise>( fn: T, timeoutMs: number, timeoutMessage?: string ): T { return ((...args: Parameters) => { return withTimeout(fn(...args) as Promise>, timeoutMs, timeoutMessage); }) as T; }