https://github.com/perry-mitchell/abort-controller-multiplexer
Combine multiple AbortControllers or AbortSignals into a single instance
https://github.com/perry-mitchell/abort-controller-multiplexer
Last synced: 5 months ago
JSON representation
Combine multiple AbortControllers or AbortSignals into a single instance
- Host: GitHub
- URL: https://github.com/perry-mitchell/abort-controller-multiplexer
- Owner: perry-mitchell
- License: mit
- Created: 2023-11-14T07:25:28.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-14T09:29:57.000Z (over 2 years ago)
- Last Synced: 2025-10-02T00:59:57.871Z (9 months ago)
- Language: JavaScript
- Size: 147 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# AbortController Multiplexer
> Combine multiple AbortControllers or AbortSignals into a single instance
[](https://www.npmjs.com/package/abort-controller-multiplexer)  
`AbortController`s are very useful mechanisms in JavaScript that allow one to _insert_ abort signals into application components with ease. You can pass an `AbortController`'s `signal` (`AbortSignal`) into methods you call, abort it later, and use it to:
* Cancel asynchronous operations
* Cancel requests
* Throw if the signal was previously aborted
* _Etc._
Sometimes, in sufficiently complicated applications, you may run across situations where you need multiple abort signals. This library can be used to **group them together** into one large signal/controller, that you can abort from a single method or watch using a single signal.
## Installation
Install using npm:
```shell
npm install abort-controller-multiplexer --save
```
_Note that this library uses ESM. You must consume it in an ESM-friendly environment._
## Usage
Combine several abort controllers:
```typescript
import { combineControllers } from "abort-controller-multiplexer";
const ac1 = new AbortController();
const ac2 = new AbortController();
const controller = combineControllers(ac1, ac2);
controller.abort();
```
_Note that `combineControllers` calls `combineSignals` under the hood._
Combine several signals:
```typescript
import { combineSignals } from "abort-controller-multiplexer";
const ac1 = new AbortController();
const ac2 = new AbortController();
const signal = combineSignals(ac1.signal, ac2.signal);
ac1.abort();
signal.throwIfAborted(); // Throws
```
Note that these combination methods _do not_ modify the underlying `AbortController` and `AbortSignal` instances.