abort-utils
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

abort-utils

Utility functions to use and combine AbortSignal and AbortController with Promises

Install

npm install abort-utils
import {
	onAbort,
	mergeSignals,
	linkControllers,
	promiseFromSignal,
	ReusableAbortController
} from 'abort-utils';

Usage

This package exports various utilities, just import what you need.

  • onAbort: Add multiple callbacks, observers and other abort controllers to a single signal.
  • mergeSignals: Create a new signal that aborts when any of the input signals aborts.
  • linkControllers: Link multiple controllers so that when one aborts, they all abort with the same reason.
  • signalFromEvent: Create an AbortSignal that is aborted when an event is dispatched.
  • signalFromPromise: Create an AbortSignal that is aborted when a Promise is resolved or rejected.
  • promiseFromSignal: Create a Promise that resolves or rejects when the signal aborts.
  • ReusableAbortController: Like AbortController, but it generates a new signal after it's aborted.

Demo

Here's a visual representation that might help understand how some of the methods differ and behave.

License

MIT © Federico Brigante

Dependents (1)

Package Sidebar

Install

npm i abort-utils

Weekly Downloads

897

Version

1.2.0

License

MIT

Unpacked Size

21.8 kB

Total Files

49

Last publish

Collaborators

  • fregante