deadlynx

NewAbortSignal helpers for Node, Bun, Deno and browsers

Timeouts and cancellation that never leak.

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.

$ npm i deadlynx {{ copied ? T.copied : '' }}
1.7kBmin + gzip
0dependencies
82tests
100%line coverage

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 P4Browsers

The problem

Why it exists

Three bugs hide in almost every hand written timeout. Each one is small, and each one ships.

Incident 01

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.

Incident 02

Abandoned is not cancelled

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.

Incident 03

Close enough is not correct

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

Small on purpose, exact on details

Seven helpers built on one rule: whatever starts a timer or adds a listener also removes it.

01

Zero leaks, tested

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.

  • settles before the timeout0 alive
  • times out0 alive
  • aborted by the caller0 alive
  • child process exits on timeexit
  1. 02

    Reasons as is

    signal.reason travels untouched: an Error, a string, null, the same object every time.

  2. 03

    Platform errors

    Timeouts reject with a TimeoutError and aborts with an AbortError, real DOMException instances with the right codes.

  3. 04

    Native first

    Uses AbortSignal.any and AbortSignal.timeout when they are exact, and falls back cleanly when they are missing.

  4. 05

    Long delays done right

    Delays above 2^31 - 1 ms are split into several timers instead of firing after 1 ms, which is what plain setTimeout does.

  5. 06

    Tiny and typed

    1.7 kB minified and gzipped, zero dependencies, ESM and CommonJS, hand written types, tree shakeable named exports.

How to

How to add a timeout to a promise or fetch

Three steps, the same in Node, Bun, Deno and the browser. It works for any promise, not only fetch.

  1. 1
    Box 1

    Install the package

    One small package with no dependencies.

    terminal
    1npm install deadlynx
  2. 2
    Box 2

    Give the work a signal

    Pass a function: it receives an AbortSignal, so the request really stops when time is up.

    report.js
    1import { withTimeout } from 'deadlynx';
    2
    3const res = await withTimeout(
    4  (signal) => fetch(url, { signal }),
    5  5000,
    6);
  3. 3
    Box 3

    Handle the timeout

    A timeout rejects with a TimeoutError. Your own abort reason comes through unchanged.

    report.js
    1import { isTimeoutError } from 'deadlynx';
    2
    3try {
    4  await loadReport();
    5} catch (error) {
    6  if (isTimeoutError(error)) showRetry();
    7  else throw error;
    8}

Live playground

Watch the timers die

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.

Request takes{{ pg.duration }} ms
Timeout after{{ pg.timeout }} ms

Tip: press Abort while it runs, or pick a timeout shorter than the request.

{{ lane.title }}
{{ lane.note }}
{{ lane.statusText }} {{ lane.leaks }} outlived the result nothing left behind
settled
{{ row.label }}
{{ lane.timers }} timers alive {{ lane.listeners }} listeners alive

Same answers as the platform

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.

CasedeadlynxNativeMatch
{{ c.name }} {{ c.ours }} {{ c.native }} {{ c.verdict }}

Examples

Code you can paste

Each helper does one thing. Mix them.

  • C1
    withTimeoutA deadline for one call, and a signal that stops the work.
  • C2
    deadlineOne time budget shared by several steps.
  • C3
    anySignalCombine shutdown, timeouts and optional signals.
  • C4
    delayRetries with backoff that stop the moment you abort.
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

How it compares

Checked by reading and running p-timeout 8.1.0 and Node 24. The README has the details for each row.

How it compares
PosEntryR1R2R3R4Pts
1deadlynx4
2AbortSignal.timeout2
3p-timeout 8.11
4Promise.race0

One point per rule

  • R1Clears its timer when the work settles
  • R2Gives the work a signal to stop
  • R3Rejects with a platform TimeoutError (DOMException)
  • R4Exact for delays above 2^31 - 1 ms in Node

Performance

Benchmarks

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.

Promise with a 1 s timeout that settles first

deadlynx withTimeout
1,348,399 ops/s
p-timeout
62,814 ops/s
native AbortSignal.timeout + raceSignal
184,564 ops/s

Combine two signals

deadlynx anySignal
551,246 ops/s
native AbortSignal.any
528,959 ops/s
any-signal
1,746,639 ops/s

Create a 1 s timeout signal

deadlynx timeoutSignal
812,043 ops/s
native AbortSignal.timeout
778,555 ops/s

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.

Compatibility

  • P1
    Node.js 18+The CI matrix runs 18, 20, 22 and 24. Where AbortSignal.any is missing, the fallback is used.
  • P2
    BunTested locally with Bun 1.3 and in the CI matrix.
  • P3
    DenoThe CI matrix runs the smoke script on Deno.
  • P4
    BrowsersAny browser with abort reasons (Chrome 98, Firefox 97, Safari 15.4). Native fast paths where present.

Reference

API

Every export. Invalid arguments throw synchronously (TypeError or RangeError), everything else follows the abort reason.

01withTimeout

withTimeout(input, ms, { signal?, message? }): Promise<T>
// input: promise, value, or (signal) => promise

Settles 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.

02delay

delay(ms, { signal? }): Promise<void>

Resolves after ms. Rejects with signal.reason and clears its timer on abort.

03anySignal

anySignal(signals: Iterable<AbortSignal | null | undefined>): AbortSignal

Aborts 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.

04timeoutSignal

timeoutSignal(ms): AbortSignal

Like AbortSignal.timeout, never keeps the process alive, exact above 2^31 - 1 ms. Infinity never aborts.

05deadline

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.

06throwIfAborted

throwIfAborted(signal?): void

Throws signal.reason if the signal has aborted. Accepts undefined.

07raceSignal

raceSignal(promise, signal?): Promise<T>

Settles like the promise unless the signal aborts first. The listener is removed either way.

08errors

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.

Give every promise a deadline.

One install, zero dependencies, and the same errors the platform throws.