npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@p-vbordei/cancellable

v0.2.1

Published

Promise utilities for AbortSignal: cancellable() wrapper, withTimeout, abortable sleep, deferred, anySignal combinator. Zero dependencies.

Readme

cancellable

ci

npm downloads bundle

Promise utilities for AbortSignal. Wrap any promise so it rejects on abort, add timeouts, sleep abortably, build deferreds, combine multiple signals into one. Zero dependencies.

import { cancellable, withTimeout, sleep, deferred, anySignal, TimeoutError } from "@p-vbordei/cancellable";

await cancellable(longRunning(), abortSignal);

await withTimeout(fetch(url), 5000);   // throws TimeoutError after 5s

await sleep(1000, abortSignal);

const d = deferred<number>();
eventBus.once("done", (n) => d.resolve(n));
const result = await d.promise;

const { signal, dispose } = anySignal([userSignal, AbortSignal.timeout(10_000)]);
await doWork(signal);
dispose();

Install

npm install @p-vbordei/cancellable

Works with Node 20+, browsers, Bun, Deno. ESM + CJS.

Why

AbortSignal is the standard cancellation primitive in modern JS, but most of your code interacts with promises that don't accept one. cancellable bridges the gap:

  • Wrap any promise so it rejects when a signal aborts
  • Add deadlines with withTimeout
  • Abortable sleep without leaking zombie timers
  • Combine multiple signals so the first one to abort wins
  • Deferreds for callback-to-promise bridging

These are the four or five missing pieces every async codebase ends up reinventing.

Recipes

Add a timeout to any promise

import { withTimeout } from "@p-vbordei/cancellable";

const result = await withTimeout(slowOperation(), 5000);

Race a user-cancel signal against a fetch deadline

import { anySignal } from "@p-vbordei/cancellable";

function fetchWithUserCancel(url: string, userCancel: AbortSignal) {
  const { signal, dispose } = anySignal([userCancel, AbortSignal.timeout(10_000)]);
  return fetch(url, { signal }).finally(dispose);
}

Abortable polling

import { sleep } from "@p-vbordei/cancellable";

async function pollUntil(predicate: () => Promise<boolean>, signal: AbortSignal) {
  while (!signal.aborted) {
    if (await predicate()) return;
    await sleep(1000, signal);
  }
}

Bridge callback API to promise

import { deferred } from "@p-vbordei/cancellable";

function once<T>(emitter: EventEmitter, event: string): Promise<T> {
  const d = deferred<T>();
  emitter.once(event, (data) => d.resolve(data));
  emitter.once("error", (err) => d.reject(err));
  return d.promise;
}

const message = await once<Message>(ws, "message");

Cancel a non-AbortSignal-aware library

import { cancellable } from "@p-vbordei/cancellable";

// This third-party library doesn't accept AbortSignal:
const ac = new AbortController();
button.addEventListener("click", () => ac.abort());

try {
  const result = await cancellable(legacyLib.doWork(), ac.signal);
} catch (err) {
  if (ac.signal.aborted) console.log("user cancelled");
  else throw err;
}
// NOTE: legacyLib.doWork() still runs to completion in the background.
// cancellable only stops YOU from waiting for it.

API

cancellable(promise, signal): Promise

Wrap a promise so it rejects with signal.reason if signal aborts before the promise settles. The underlying work isn't truly canceled — only your view of it. For true cancellation, the underlying API must accept AbortSignal itself.

withTimeout(promise, ms, signal?): Promise

Reject with TimeoutError after ms. If a signal is also provided, abort fires first.

sleep(ms, signal?): Promise<void>

Like setTimeout but as a promise. Aborts cleanly on signal — no zombie timers.

deferred<T>(): Deferred<T>

type Deferred<T> = {
  promise: Promise<T>;
  resolve: (value: T) => void;
  reject: (reason?: unknown) => void;
  settled: boolean;
};

Subsequent calls to resolve / reject after the first one are no-ops. Useful for callback-to-promise bridges.

anySignal(signals[]): { signal, dispose }

Returns an AbortSignal that fires when any of the inputs aborts. Call dispose() when you no longer need it, to detach listeners from the source signals.

class TimeoutError

Thrown by withTimeout. Has a timeoutMs property.

Caveats

  • cancellable doesn't truly cancel work — only your view of it. The underlying promise continues executing. For actual cancellation, the underlying operation must accept AbortSignal (e.g., fetch, node:fs.promises, modern DB drivers).
  • anySignal allocates listeners on every input signal — call dispose() to avoid leaks in long-lived contexts.

License

Apache-2.0 © Vlad Bordei