The classic timeout leaks
Promise.race with a sleeping promise never clears its timer. In a server that is one live timer per request, and in a CLI it keeps the process running long after the work is done.
NewAbortSignal helpers for Node, Bun, Deno and browsers
Give any promise a deadline, cancel the work behind it, and leave nothing behind: every timer is cleared and every listener is removed the moment the result settles.
A code window runs withTimeout in this page with a 1000 ms limit and shows that no timer or listener is left once each run settles.
Runs wherever AbortSignal does
P1Node.js 18+ P2Bun P3Deno P4BrowsersThe problem
Three bugs hide in almost every hand written timeout. Each one is small, and each one ships.
Promise.race with a sleeping promise never clears its timer. In a server that is one live timer per request, and in a CLI it keeps the process running long after the work is done.
A timeout that only stops waiting leaves the socket open and the query running. deadlynx hands your function an AbortSignal, so the work itself stops.
Wrapped errors, swallowed reasons and timers that overflow after 24.8 days are subtle. Here the reason you abort with is the reason you get, and errors are named like the platform names them.
Features
Seven helpers built on one rule: whatever starts a timer or adds a listener also removes it.
A fake clock counts live timers, listener counts are checked after every path, and a child process must exit the moment its work is done.
signal.reason travels untouched: an Error, a string, null, the same object every time.
Timeouts reject with a TimeoutError and aborts with an AbortError, real DOMException instances with the right codes.
Uses AbortSignal.any and AbortSignal.timeout when they are exact, and falls back cleanly when they are missing.
Delays above 2^31 - 1 ms are split into several timers instead of firing after 1 ms, which is what plain setTimeout does.
1.7 kB minified and gzipped, zero dependencies, ESM and CommonJS, hand written types, tree shakeable named exports.
How to
Three steps, the same in Node, Bun, Deno and the browser. It works for any promise, not only fetch.
One small package with no dependencies.
1npm install deadlynxPass a function: it receives an AbortSignal, so the request really stops when time is up.
1import { withTimeout } from 'deadlynx';
2
3const res = await withTimeout(
4 (signal) => fetch(url, { signal }),
5 5000,
6);A timeout rejects with a TimeoutError. Your own abort reason comes through unchanged.
1import { isTimeoutError } from 'deadlynx';
2
3try {
4 await loadReport();
5} catch (error) {
6 if (isTimeoutError(error)) showRetry();
7 else throw error;
8}Live playground
Start a fake request with a timeout. The same request runs three ways, with the real library running in your browser. Every bar is a live timer or listener, recorded by wrapping setTimeout and addEventListener on this page.
Tip: press Abort while it runs, or pick a timeout shorter than the request.
Each row runs the library and the native API in this browser and compares the result. Reasons are compared by identity, errors by name and class.
No results yet. Run the checks to compare deadlynx with the native API in this browser.
| Case | deadlynx | Native | Match |
|---|---|---|---|
| {{ c.name }} | {{ c.ours }} | {{ c.native }} | {{ c.verdict }} |
Examples
Each helper does one thing. Mix them.
1import { withTimeout, isTimeoutError } from 'deadlynx';
2
3try {
4 const res = await withTimeout(
5 (signal) => fetch('/api/report', { signal }),
6 5000,
7 { signal: request.signal },
8 );
9 console.log(res.status);
10} catch (error) {
11 if (isTimeoutError(error)) console.log('slower than 5 s, fetch was aborted');
12 else throw error; // request.signal.reason, unchanged
13}Comparison
Checked by reading and running p-timeout 8.1.0 and Node 24. The README has the details for each row.
| Pos | Entry | R1 | R2 | R3 | R4 | Pts |
|---|---|---|---|---|---|---|
| 1 | deadlynx | 4 | ||||
| 2 | AbortSignal.timeout | 2 | ||||
| 3 | p-timeout 8.1 | 1 | ||||
| 4 | Promise.race | 0 |
One point per rule
Performance
Operations per second, median of 7 runs after warmup. Higher is better. The other packages were installed in a scratch folder, not in this repo.
Machine: Node v24.16.0, AMD Ryzen 7 7800X3D 8-Core Processor, Windows_NT 10.0.19045, 2026-12-04
Where deadlynx is slower: combining signals. It calls the native AbortSignal.any, which is slower than the plain listener approach of any-signal, but the native version can be garbage collected without a clear() call.
Size
1,739bytes
The whole library, minified and gzipped. No dependencies, nothing to configure.
AbortSignal.any is missing, the fallback is used.Reference
Every export. Invalid arguments throw synchronously (TypeError or RangeError), everything else follows the abort reason.
withTimeout(input, ms, { signal?, message? }): Promise<T>
// input: promise, value, or (signal) => promiseSettles like input, rejects with a TimeoutError after ms, or with signal.reason on abort. The function form receives a signal that aborts on either. message sets the error message, or pass an Error to reject with it.
delay(ms, { signal? }): Promise<void>Resolves after ms. Rejects with signal.reason and clears its timer on abort.
anySignal(signals: Iterable<AbortSignal | null | undefined>): AbortSignalAborts when any input aborts, with that input's reason. Native AbortSignal.any when available; the fallback removes all of its listeners on the first abort. null entries are skipped.
timeoutSignal(ms): AbortSignalLike AbortSignal.timeout, never keeps the process alive, exact above 2^31 - 1 ms. Infinity never aborts.
deadline(ms, { signal? }): { signal, remaining(), elapsed(), throwIfAborted(), clear() }A signal that aborts at the deadline or with the parent, plus remaining(), elapsed(), throwIfAborted() and clear(). Works with using.
throwIfAborted(signal?): voidThrows signal.reason if the signal has aborted. Accepts undefined.
raceSignal(promise, signal?): Promise<T>Settles like the promise unless the signal aborts first. The listener is removed either way.
createTimeoutError(message?), createAbortError(message?)
isTimeoutError(error), isAbortError(error)Create and detect the platform errors. Detection checks the name, so errors from fetch and other realms count too.
Use it when it fits. But it cannot be cleared, so its timer keeps running after your request finished, and in Node a delay above 2^31 - 1 ms fires after about 1 ms. withTimeout clears its timer on settle and timeoutSignal stays exact for long delays.
Timeouts and cancellation that never leak.
One install, zero dependencies, and the same errors the platform throws.