MCPcopy Create free account
hub / github.com/node-modules/utility / promiseTimeout

Function promiseTimeout

src/timeout.ts:13–29  ·  view source on GitHub ↗
(
  promiseArg: Promise<T>,
  timeout: number,
)

Source from the content-addressed store, hash-verified

11
12// https://betterstack.com/community/guides/scaling-nodejs/nodejs-timeouts/
13export async function promiseTimeout<T>(
14 promiseArg: Promise<T>,
15 timeout: number,
16): Promise<T> {
17 let timer: NodeJS.Timeout;
18 const timeoutPromise = new Promise<never>((_, reject) => {
19 timer = setTimeout(() => {
20 reject(new TimeoutError(timeout));
21 }, timeout);
22 });
23
24 try {
25 return await Promise.race([ promiseArg, timeoutPromise ]);
26 } finally {
27 clearTimeout(timer!);
28 }
29}
30
31export async function runWithTimeout<T>(
32 scope: () => Promise<T>,

Callers 2

timeout.test.tsFile · 0.85
runWithTimeoutFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…