/** Rejects with `message` if `promise` has not settled within `ms`. */ export async function withTimeout( promise: Promise, ms: number, message: string, ): Promise { let timer: NodeJS.Timeout; const timeoutPromise = new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(message)), ms); }); try { return await Promise.race([promise, timeoutPromise]); } finally { clearTimeout(timer!); } }