@esfx/cancelable
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@esfx/cancelable

The @esfx/cancelable package provides a low-level Symbol-based API for defining a common cancellation protocol.

NOTE: This package does not contain an implementation of cancellation signals, but rather provides only a protocol for interoperable libraries that depend on cancellation.

For an implementation of this protocol, please consider the following packages:

Overview

Installation

npm i @esfx/cancelable

Usage

import { Cancelable } from "@esfx/cancelable";

function doSomeWork(cancelable: Cancelable) {
    return new Promise((resolve, reject) => {
        const cancelSignal = cancelable[Cancelable.cancelSignal]();
        if (cancelSignal.signaled) throw new Error("Operation canceled.");

        const child = fork("worker.js");
        const subscription = cancelSignal.subscribe(() => {
            // cancellation requested, abort worker
            worker.kill();
            reject(new Error("Operation canceled."));
        });

        worker.on("exit", () => {
            subscription.unsubscribe();
            resolve();
        });
    });
}

API

You can read more about the API here.

Readme

Keywords

none

Package Sidebar

Install

npm i @esfx/cancelable

Weekly Downloads

12,856

Version

1.0.0

License

Apache-2.0

Unpacked Size

52.4 kB

Total Files

8

Last publish

Collaborators

  • rbuckton