JSPM

recursive-cancelable-promise

3.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q37670F
  • License MIT

A CancelablePromise with the ability to stop a CancelablePromise created in a CancelablePromise

Package Exports

  • recursive-cancelable-promise
  • recursive-cancelable-promise/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (recursive-cancelable-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

recursive-cancelable-promise

A CancelablePromise with the ability to stop a CancelablePromise created in a CancelablePromise. Can be created from: CancelablePromise.

Install

npm install recursive-cancelable-promise
yarn add recursive-cancelable-promise

Examples

RecursiveCancelablePromise

import RecursiveCancelablePromise, { RCPController } from 'recursive-cancelable-promise';

const recursiveCancelablePromise  = 
    new RecursiveCancelablePromise(
        // try handle
        async (controller: RCPController): Promise</*type*/> => {
            // do smth

            // abort execution, if canceled
            controller.sync();

            // abort execution and do smth, if cancled
            controller.sync(() => {
                // do smth
            });
    
            // return true if canceled
            controller.isCanceled();

            // subscribe new RecursiveCancelablePromise to parent RecursiveCancelablePromise
            // if parent canceled, child will be canceled too
            // if child canceled, parent will be not canceled
            await controller.subscribe(() => new RecursiveCancelablePromise(/*...*/));
        },
        // catch handle, not required parameter
        async (controller: RCPController, error): Promise</*type*/> => {
            // error - error throwed from try handle,
            // if catch handle not specified RecursiveCancelablePromise will be rejected with error

            // do smth

            // controller identical to the controller from try handle
        },
        // do when cancel called, not required parameter
        async (): Promise<void> => {
            // do smth
        }
    );

wrapCancelablePromise

import { wrapCancelablePromise } from 'recursive-cancelable-promise';
import CancelablePromise from 'cancelable-promise';

const recursiveCancelablePromise = wrapCancelablePromise(
        new CancelablePromise((resolve, reject, onCancel) => {
            // do smth
        }),
    );

RCPCancelError

import RecursiveCancelablePromise, { RCPController } from 'recursive-cancelable-promise';

const recursiveCancelablePromise = new RecursiveCancelablePromise(...);

...

try {
    await recursiveCancelablePromise;
} catch(error) {
    if (error instanceof RCPCancelError) {
        // handle canceled
    }
}