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

1.1.0 • Public • Published

@billogram/cancelable-promise

A function for making a global promise "cancelable"

Install

yarn add @billogram/cancelable-promise

Usage

import {
    makeCancelablePromise,
} from '@billogram/cancelable-promise';

See types for exported types.

makeCancelablePromise

import {
    Data,
} from './types';


const promise = new Promise((resolve, reject) => {
    /* ... */
});

const [cancelablePromise, cancel] = makeCancelablePromise(promise);

try {
    const token: CancellationFulfillmentToken<Data> = await cancelablePromise;

    if (!token.isPromiseCanceled) {
        const data = token.value;

        // ...
    }
} catch (e) {
    const token = e as CancellationRejectionToken<unknown>;

    if (!token.isPromiseCanceled) {
        const reason = token.reason;

        // ...
    }
}

Note

This library is being published with our use cases in mind and is not necessarily meant to be consumed by the broader public. We probably won't take your feature requests unless they align with our own needs.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @billogram/cancelable-promise

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

9.84 kB

Total Files

16

Last publish

Collaborators

  • jamesbillo
  • oscarekholm
  • irynaziruikina
  • jmfederico
  • andreagylling
  • ewakosciukiewicz
  • miriamlarsson