Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marvinhagemeister/cancel-token
https://github.com/marvinhagemeister/cancel-token
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/marvinhagemeister/cancel-token
- Owner: marvinhagemeister
- License: mit
- Created: 2018-04-15T06:16:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-25T17:44:34.000Z (over 5 years ago)
- Last Synced: 2024-07-01T00:26:51.329Z (3 months ago)
- Language: TypeScript
- Size: 136 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Simple async promise cancellation
Minimal implementation for cancel tokens, similar to `AbortController`, but
without the event handling. Uses the `throw` keyword under the hood but throws
a `string` to prevent expensive stack trace creation.## Installation
```bash
npm install @marvinh/cancel-token
# or
yarn add @marvinh/cancel-token
```## Usage
```js
import { CancelController } from "@marvinh/cancel-token";const controller = new CancelController();
const signal = controller.signal;doSomethingAsync()
.then(() => {
if (signal.aborted) throw "Aborted";
// do something else
})
.catch(err => {
if (err === "Aborted") {
console.log("was aborted");
}
});
```The above can be simplified with a helper function:
```js
import { CancelController, wrap } from "@marvinh/cancel-token";const controller = new CancelController();
const signal = controller.signal;wrap(doSomethingAsync(), signal).catch(err => {
if (err === "Aborted") {
console.log("was aborted");
}
});
```## License
`MIT`, see the [license file](./LICENSE.md).