@billogram/cancelable-promise

A function for making a global promise "cancelable"

Usage no npm install needed!

<script type="module">
  import billogramCancelablePromise from 'https://cdn.skypack.dev/@billogram/cancelable-promise';
</script>

README

@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