Download src/utils/with-timeout.ts from shimen/shinclaw: direct link, hf CLI and curl.
- Browser
- Download file 433 Bytes
-
https://huggingface.co/spaces/shimen/shinclaw/resolve/main/src/utils/with-timeout.ts
- Command line
-
hf download hf://spaces/shimen/shinclaw/src/utils/with-timeout.ts
-
curl -L -o with-timeout.ts https://huggingface.co/spaces/shimen/shinclaw/resolve/main/src/utils/with-timeout.ts
433 Bytes
| export function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> { | |
| if (!timeoutMs || timeoutMs <= 0) { | |
| return promise; | |
| } | |
| let timer: NodeJS.Timeout | null = null; | |
| const timeout = new Promise<T>((_, reject) => { | |
| timer = setTimeout(() => reject(new Error("timeout")), timeoutMs); | |
| }); | |
| return Promise.race([promise, timeout]).finally(() => { | |
| if (timer) { | |
| clearTimeout(timer); | |
| } | |
| }); | |
| } | |