Miss any of our Open RFC calls?Watch the recordings here! »

cancellation-wrapper

1.0.1 • Public • Published

Small wrapper for accepting cancellation tokens

Wraps your api methods and functions and make possible to pass a cancellable tokens.

Examples:

  1. If your function returns a PromiseLike object and you need to cancel it by passing cancellation token as a last argument, than you can use wrapFn function.
import { wrapFn, CancellationToken } from "wrapper";
 
const yourFn = (a, b) => new Promise(res => {
    res(+ b);
});
 
const cancellableYourFn = wrapFn(yourFn);
const token = new CancellationToken();
const result = cancellableYourFn(1, 2, token).then(
    res => res + 1,
    err => 0
).then(
    res => console.log(res) // 0 will be in console
);
 
// in a few moments you need to abort your action
token.cancel();
  1. If you don't want to mess up with every function and you have some API obejct with list of methods that returns a PromiseLike object and you need to cancel it by passing cancellation token as a last argument, than you can use wrap function.
import { wrap, CancellationToken } from "wrapper";
 
const yourApi = {
    add: (a, b) => new Promise(res => {
        res(+ b);
    }),
    multiply: (a, b) => new Promise(res => {
        res(* b);
    }),
    power: (a, b) => new Promise(res => {
        res(** b);
    })
}
 
const cancellableYourApi = wrap(yourApi);
const token = new CancellationToken();
const result = cancellableYourApi.multiply(1, 2, token).catch(
    err => 0
).then(
    res => console.log(res) // 0 will be in console
);
 
// in a few moments you need to abort your action
token.cancel();

For Node.js you can use npm package. You should install it:

    npm i cancellation-wrapper

and import it in your file:

const { wrapFn, CancellationToken } = require("wrapper");

Install

npm i cancellation-wrapper

DownloadsWeekly Downloads

0

Version

1.0.1

License

Apache-2.0

Unpacked Size

24 kB

Total Files

10

Last publish

Collaborators

  • avatar