p-timeout
Adds a timeout to a promise, optionally returning a fallback value or invoking an abort signal.
Official Documentation ↗p-timeout v6.x(ESM)/ Node 18 LTS. Check the official docs for the latest API surface.Installation
npm install p-timeoutpnpm add p-timeoutyarn add p-timeoutMethods
No methods match this filter.
Overview
p-timeout races a promise against a timer. If the timer wins it rejects with a TimeoutError, or resolves with a fallback when one is provided, which is useful for best-effort operations.
Core Methods
The primary call takes a promise and a duration. An options object accepts a signal or a custom message, and a value or function can be supplied as a fallback.
Usage Notes
Wrap slow I/O with a timeout so a hung dependency cannot stall a request forever. Pair the timeout with a real AbortSignal on the inner operation to free resources.
Common Pitfalls
Without an abort signal the wrapped work continues in the background and may still mutate state. Very short timeouts cause false negatives on cold starts or slow networks, so tune per environment.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/sindresorhus/p-timeout@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:33.113Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Cancellation is best-effort: if .cancel() throws, the error is ignored and the promise still settles as usual. (https://cdn.jsdelivr.net/gh/sindresorhus/p-timeout@latest/readme.md)
FAQ
- Does pTimeout cancel the underlying promise?
- No. It only rejects your await; the original promise keeps running unless you also pass an AbortSignal to the operation so it can stop its own work.
- What error is thrown on timeout?
- A TimeoutError from this package is thrown. Check the constructor or the message to distinguish a timeout from a genuine rejection of the wrapped promise.
- Can I use a fallback function?
- Pass a function as the fallback and it is invoked lazily on timeout, so you can compute the fallback only when needed instead of eagerly.
Report a problem with this API docOpens a pre-filled issue on GitHub.