cancellation-wrapper
v1.0.1
Published
Wraps your api methods and functions and make possible to pass a cancellation token for promises
Downloads
1
Readme
Small wrapper for accepting cancellation tokens
Wraps your api methods and functions and make possible to pass a cancellable tokens.
Examples:
- If your function returns a
PromiseLike
object and you need to cancel it by passing cancellation token as a last argument, than you can usewrapFn
function.
import { wrapFn, CancellationToken } from "wrapper";
const yourFn = (a, b) => new Promise(res => {
res(a + 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();
- 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 usewrap
function.
import { wrap, CancellationToken } from "wrapper";
const yourApi = {
add: (a, b) => new Promise(res => {
res(a + b);
}),
multiply: (a, b) => new Promise(res => {
res(a * b);
}),
power: (a, b) => new Promise(res => {
res(a ** 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");