UNPKG

@rdfc/http-utils-processor-ts

Version:
15 lines (14 loc) 421 B
export async function timeout(ms, promise) { if (ms === null || ms === undefined) { return promise; } let timeoutHandle; const timeoutPromise = new Promise((_, reject) => { timeoutHandle = setTimeout(() => reject(), ms); }).catch(() => { throw "timeout"; }); return Promise.race([promise, timeoutPromise]).finally(() => { clearTimeout(timeoutHandle); }); }