Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 128x 7x 7x 7017x 959x 959x 2x 958x 959x 959x 959x 959x 959x 959x | /** Schedule a timeout and return a function that cancels it. */
export type ScheduleTimeout = (callback: () => void, timeoutMs: number) => () => void;
const scheduleHostTimeout: ScheduleTimeout = (callback, timeoutMs) => {
const timer = setTimeout(callback, timeoutMs);
return () => clearTimeout(timer);
};
/**
* Wait for cleanup to settle, swallowing its rejection. Return false only when the timeout wins.
* Timing out stops waiting; the cleanup task continues. Always cancel the timeout after the wait.
*/
export async function settleWithTimeout(
task: void | Promise<void> | undefined,
timeoutMs: number,
scheduleTimeout: ScheduleTimeout = scheduleHostTimeout,
): Promise<boolean> {
if (!task) return true;
let cancelTimeout: (() => void) | undefined;
let completed = false;
const guardedTask = Promise.resolve(task)
.catch(() => undefined)
.then(() => {
completed = true;
});
const timeout = new Promise<void>((resolve) => {
cancelTimeout = scheduleTimeout(resolve, timeoutMs);
});
try {
await Promise.race([guardedTask, timeout]);
return completed;
} finally {
cancelTimeout?.();
}
}
|