UNPKG

@nostr-dev-kit/ndk

Version:

NDK - Nostr Development Kit. Includes AI Guardrails to catch common mistakes during development.

16 lines (15 loc) 533 B
/** * Run a promise with a timeout if one is provided. * @returns */ export async function runWithTimeout<T>(fn: () => Promise<T>, timeoutMs?: number, timeoutMessage?: string): Promise<T> { if (!timeoutMs) return fn(); return new Promise<T>((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error(timeoutMessage || `Timed out after ${timeoutMs}ms`)); }, timeoutMs); fn() .then(resolve, reject) .finally(() => clearTimeout(timeout)); }); }