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

0.1.0 • Public • Published

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:

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:

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.

Readme

Keywords

Package Sidebar

Install

npm i cancellor

Weekly Downloads

334

Version

0.1.0

License

MIT

Unpacked Size

6.08 kB

Total Files

10

Last publish

Collaborators

  • perrymitchell