# @esfx/canceltoken

> Proviced CancelToken a cancellation primitive.

Latest version **1.0.0** (published 2022-10-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @esfx/canceltoken
pnpm add @esfx/canceltoken
yarn add @esfx/canceltoken
bun add @esfx/canceltoken
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-10-20 |
| First published | 2022-09-05 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 608.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 234 |
| Author | Ron Buckton |
| Maintainers | rbuckton |

## Links

- npm: https://www.npmjs.com/package/@esfx/canceltoken
- Repository: https://github.com/esfx/esfx
- Homepage: https://github.com/esfx/esfx#readme
- Issues: https://github.com/esfx/esfx/issues
- npm.io page: https://npm.io/package/@esfx/canceltoken

## Dependencies (3)

- [tslib](https://npm.io/package/tslib.md) ^2.4.0
- [@esfx/cancelable](https://npm.io/package/@esfx/cancelable.md) ^1.0.0
- [@esfx/disposable](https://npm.io/package/@esfx/disposable.md) ^1.0.0

## Recent versions

- 1.0.0 (latest) — 2022-10-20
- 1.1.0-alpha.202307260322 (next) — 2023-07-26
- 1.0.0-dev.8 (dev) — 2022-10-20
- 1.1.0-alpha.202306080253 — 2023-06-08
- 1.1.0-alpha.202306061406 — 2023-06-06
- 1.1.0-alpha.202306061316 — 2023-06-06
- 1.1.0-alpha.202306051228 — 2023-06-05
- 1.1.0-alpha.202305082104 — 2023-05-08
- 1.1.0-alpha.202302091715 — 2023-02-09
- 1.1.0-alpha.202302071935 — 2023-02-07
- 1.1.0-alpha.202211291446 — 2022-11-29
- 1.1.0-alpha.202211221509 — 2022-11-22
- 1.1.0-alpha.202210252139 — 2022-10-25
- 1.0.0-dev.7 — 2022-10-20
- 1.0.0-dev.6 — 2022-10-19
- … 18 more at https://npm.io/package/@esfx/canceltoken/versions

## README

# `@esfx/canceltoken`

The `@esfx/canceltoken` package provides the CancelToken class, an implementation of `@esfx/cancelable`.

# Overview

* [Installation](#installation)
* [Usage](#usage)
* [API](#api)

# Installation

```sh
npm i @esfx/canceltoken
```

# Usage

* [Basic Usage](#basic-usage)
* [Linking Tokens](#linking-tokens)

## Basic Usage

```ts
import { CancelToken } from "@esfx/canceltoken";

// consume a cancel token
async function doWork(token = CancelToken.none) {
    // do some work
    await doSomeOtherWork(token);

    // throw an error if cancellation has been signaled since awaiting.
    token.throwIfSignaled();
}

function doSomeOtherWork(token = CancelToken.none) {
    return new Promise((resolve, reject) => {
        token.throwIfSignaled(); // throw if cancellation has already been signaled.

        // setup some external async operation...
        const worker = ...;

        // listen for cancellation and abort the worker.
        const subscription = token.subscribe(() => {
            worker.abort();
            reject(new CancelError());
        });
        
        // start working, resolve when done
        worker.start(resolve);
    });
}

// call an async function that supports cancellation
const source = CancelToken.source();
doWork(source.token).then(() => {
    // operation completed...
    source.close();
}, err => {
    if (err instanceof CancelError) {
        // operation was canceled..
    }
});

// cancel operation after 10 seconds
setTimeout(() => source.cancel(), 1000 * 10);
```

## Linking Tokens

```ts
import { CancelToken } from "@esfx/canceltoken";

// You can compose a cancellation graph with a root CancelToken, allowing you to cancel a large
// number of asynchronous operations all at once

let rootSource = CancelToken.source();

function cancelAllDownloads() {
    // explicitly cancel all downloads
    rootSource.cancel();

    // reset the root source
    rootSource = CancelToken.source();
}

async function downloadFile(url, path, token = CancelToken.none) {
    // add own source for timeout after 60 seconds
    const timeoutSource = CancelToken.source();
    setTimeout(() => timeoutSource.cancel(), 60 * 1000);

    // download can be canceled by either 'rootSource', 'timeoutSource' or 'token':
    const linkedToken = CancelToken.race([rootSource.token, timeoutSource.token, token]);

    // ... use linkedToken to observe cancellation.
}
```

# API

You can read more about the API [here](https://esfx.js.org/esfx/api/canceltoken.html).

---
_Source: https://npm.io/package/@esfx/canceltoken · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
