Hierarchical cancellation scopes for modern JavaScript runtimes.
Built on top of the platform's native AbortController / AbortSignal, @abortor/core makes cancellation composable, structured, and developer‑friendly.
scope.run(fn) and it automatically tracks lifetime.scope.fetch() threads the scope's signal into fetch calls.scope.timeout(ms) or combine signals with composeAny().AbortSignal.any() and AbortSignal.timeout() if available; provides shims otherwise.npm install @abortor/core
# or
pnpm add @abortor/core
yarn add @abortor/core
Requires Node.js 18+ or modern browsers with Abort APIs.
import { scope } from "@abortor/core";
// Create a new scope (optionally label it)
const s = scope({ label: "SearchBox" });
// Run async work inside the scope
const result = await s.run(async (sc) => {
const res = await sc.fetch("https://jsonplaceholder.typicode.com/users");
return res.json();
});
// Later, cancel everything under this scope
s.dispose("navigated away");
scope(opts?)Creates a new cancellation scope.
const s = scope({ label: string, parent: Scope });
A Scope exposes:
signal: AbortSignal — propagate into APIs like fetchrun(fn) — run async work bound to the scopechild(opts?) — create a nested scopedispose(reason?) — cancel this scope and all childrenfetch(input, init?) — like fetch, but auto‑uses the scope’s signaltimeout(ms, reason?) — create a timeout signal combined with this scoperace(promises) / all(promises) — helpers that cancel correctlywithScope(fn, opts?)Utility to create a scope, run work, and auto‑dispose.
import { withScope } from "@abortor/core";
await withScope(async (sc) => {
await sc.fetch("/api/data");
}); // auto‑disposed here
import { configureDiagnostics } from "@abortor/core";
configureDiagnostics({
warnings: true,
strict: false,
perf: false,
});
composeAny(signals: AbortSignal[]): AbortSignalCombine multiple signals; aborts when any does.
withTimeout(ms: number, parent?: AbortSignal, reason?: string): AbortSignalCreate a timeout signal, optionally combined with a parent.
Also see:
@abortor/angular – Angular-specific integration with component lifecycle-aware cancellation.
@abortor/react — React hooks and utilities for cancellation-aware components (requires React 18+).