https://github.com/perry-mitchell/cancellor
Cancel token generator
https://github.com/perry-mitchell/cancellor
Last synced: 5 months ago
JSON representation
Cancel token generator
- Host: GitHub
- URL: https://github.com/perry-mitchell/cancellor
- Owner: perry-mitchell
- License: mit
- Created: 2021-08-30T07:59:10.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-11-21T12:18:13.000Z (over 3 years ago)
- Last Synced: 2025-10-21T16:38:45.520Z (9 months ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Cancellor
> Cancel token generator
Generate super-simple cancel "tokens" that can be used to flag the cancellation of sync and async tasks. Use tokens to detect the need to exit early or throw an exception in one line if a token has been cancelled.
## Why?
There's a good number of cancel token libraries out there, but many of the maintained ones make use of classes and the like which transpile poorly. I needed something simple and that leaves a very small footprint.
## Installation
Simply install using `npm install cancellor --save`.
## Usage
**Cancellor** exports a `createToken` method:
```typescript
import { CancelToken, createToken } from "cancellor";
const token: CancelToken = createToken();
token.isCancelled(); // false
token.cancel();
token.isCancelled(); // true
token.throwIfCancelled(); // Error: Token was cancelled
```
Cancel tokens also emit events:
```typescript
const removeListener = token.onCancel(() => {
// Fired when `token.cancel()` is called
});
// later
token.cancel();
```
## Development
The library is written in typescript and should be developed against NodeJS 14 or newer.
Make sure to install all dependencies (`npm install`) before running any other command. You can build the final source using `npm run build`. Run tests with `npm test`.