export async function withTimeout(promise: Promise, ms: number, message: string): Promise { // The Promise constructor runs its executor synchronously, so `handle` is // always assigned before the `finally` below ever runs. let handle: ReturnType const timeout = new Promise((_, reject) => { handle = setTimeout(() => reject(new Error(message)), ms) }) try { return await Promise.race([promise, timeout]) } finally { clearTimeout(handle!) } }