https://github.com/toebeann/signals
A collection of wrappers and utility functions for working with AbortSignals.
https://github.com/toebeann/signals
abort abortcontroller abortsignal aggregate-signal aggregatesignal any-signal anysignal is-abort-signal is-signal isabortsignal issignal javascript nodejs npm signal timeout timeout-signal timeoutsignal typescript
Last synced: 10 months ago
JSON representation
A collection of wrappers and utility functions for working with AbortSignals.
- Host: GitHub
- URL: https://github.com/toebeann/signals
- Owner: toebeann
- License: mit
- Created: 2022-08-20T11:16:53.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-01T15:05:00.000Z (11 months ago)
- Last Synced: 2025-04-08T05:52:17.163Z (11 months ago)
- Topics: abort, abortcontroller, abortsignal, aggregate-signal, aggregatesignal, any-signal, anysignal, is-abort-signal, is-signal, isabortsignal, issignal, javascript, nodejs, npm, signal, timeout, timeout-signal, timeoutsignal, typescript
- Language: TypeScript
- Homepage: https://toebeann.github.io/signals
- Size: 1.31 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# signals 🚥
A collection of wrappers and utility functions for working with [AbortSignals](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal).
[](https://npmjs.org/package/@toebean/signals "View signals on npm") [](https://npmjs.org/package/@toebean/signals "View signals on npm") [](https://toebeann.github.io/signals "Read the documentation on Github Pages") [](https://github.com/toebeann/signals/blob/main/LICENSE "View the license on GitHub")
[](https://codecov.io/gh/toebeann/signals "View code coverage on Codecov") [](https://www.codefactor.io/repository/github/toebeann/signals "View code quality on CodeFactor") [](https://bundlephobia.com/package/@toebean/signals "View signals on Bundlephobia") [](https://bundlephobia.com/package/@toebean/signals "View signals on Bundlephobia")
[](https://github.com/toebeann/signals/actions/workflows/npm-test.yml "View npm test on GitHub Actions") [](https://github.com/toebeann/signals/actions/workflows/publish-code-coverage.yml "View publish code coverage on GitHub Actions") [](https://github.com/toebeann/signals/actions/workflows/publish-package.yml "View publish package on GitHub Actions") [](https://github.com/toebeann/signals/actions/workflows/publish-docs.yml "View publish docs on GitHub Actions")
[](https://github.com/toebeann/signals "View signals on GitHub") [](https://twitter.com/toebean__ "Follow @toebean__ on Twitter") [](https://github.com/sponsors/toebeann "Sponsor signals on GitHub") [](https://paypal.me/tobeyblaber "Donate to signals with PayPal")
## Table of contents
- [signals 🚥](#signals-)
- [Table of contents](#table-of-contents)
- [Install](#install)
- [npm](#npm)
- [Usage](#usage)
- [AggregateSignal](#aggregatesignal)
- [TimeoutSignal](#timeoutsignal)
- [isAbortSignal](#isabortsignal)
- [API reference](#api-reference)
- [Quick links](#quick-links)
- [License](#license)
## Install
### [npm](https://www.npmjs.com/package/@toebean/signals "npm is a package manager for JavaScript")
```text
npm i @toebean/signals
```
## Usage
### AggregateSignal
Combines several [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) instances into a signal which will be aborted as soon as any of the given signals are.
```js
import { AggregateSignal } from "@toebean/signals";
const ac = new AbortController();
const aggregateSignal = new AggregateSignal(ac.signal, someOtherSignal);
// passing an aggregate signal into an awaitable, abortable call:
await doSomeAbortableAction({ signal: aggregateSignal.signal });
// determining which of the original signals was aborted first:
switch (aggregateSignal.abortedSignal) {
case ac.signal:
// do stuff
break;
// etc...
}
```
### TimeoutSignal
Creates an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) which will timeout after a given number of milliseconds, using [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) under the hood.
```js
import { AggregateSignal } from "@toebean/signals";
const timeoutSignal = new TimeoutSignal(200); // creates an AbortSignal which will abort in 200ms
// passing a timeout signal into an awaitable, abortable call:
await doSomeAbortableAction({ signal: timeoutSignal.signal });
// If for whatever reason you need to clear the underlying timeout of the TimeoutSignal, you can:
clearTimeout(timeoutSignal.timeout);
```
### isAbortSignal
A TypeScript type guard for checking whether a given object is an `AbortSignal`.
```ts
import { isAbortSignal } from "@toebean/signals";
if (isAbortSignal(someObject)) {
// within this block, someObject is typed as an AbortSignal
console.log(someObejct.aborted);
}
```
## API reference
The full API reference for signals is [available on GitHub Pages](https://toebeann.github.io/signals).
### Quick links
- [AggregateSignal](https://toebeann.github.io/signals/stable/classes/AggregateSignal.html)
- [TimeoutSignal](https://toebeann.github.io/signals/stable/classes/TimeoutSignal.html)
- [isAbortSignal](https://toebeann.github.io/signals/stable/functions/isAbortSignal.html)
## License
signals is licensed under [MIT](https://github.com/toebeann/signals/blob/main/LICENSE) © 2022 Tobey Blaber.