export async function withTimeout( promise: Promise, timeoutMs: number, message: string ): Promise { let timeoutId: ReturnType | undefined = undefined; const timeoutPromise = new Promise((_resolve, reject) => { timeoutId = setTimeout(() => { reject(new Error(message)); }, timeoutMs); }); return Promise.race([promise, timeoutPromise]).finally(() => { clearTimeout(timeoutId); }); }