-
Notifications
You must be signed in to change notification settings - Fork 0
async.Function.timeout
github-actions[bot] edited this page Jun 8, 2026
·
2 revisions
@zenstone/ts-utils / async / timeout
timeout<
F>(fn,ms): (...args) =>Promise<Awaited<ReturnType<F>>>
Defined in: src/async/index.ts:231
包装函数,添加超时控制
返回的函数保持原函数的参数签名,返回值统一为 Promise。 如果执行超过指定时间,将抛出 TimeoutError。
F extends AnyFn
F
要包装的函数(同步或异步)
number
超时毫秒数
包装后的函数
(...args) => Promise<Awaited<ReturnType<F>>>
const fetchWithTimeout = timeout(
(url: string) => fetch(url).then(r => r.json()),
5000,
);
const data = await fetchWithTimeout('/api/data');