1.1.0 • Published 3 years ago

@billogram/cancelable-promise v1.1.0

Weekly downloads
3
License
MIT
Repository
-
Last release
3 years ago

@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