Cancellable Chain Of Promises
A library to write cancellable chain of promises.
This library is inspired by the This-Binding Syntax proposal. If you don't want to depend on this proposal, you can have a look at the like-bind-operator library.
Examples
; const token = { cancelButtononclick = cancel;}; // Write your asynchronous code:const promise = token ::token ::token ::token; // If you cancel, the "promise" object will reject with an Cancelled error.
like-bind-operator
Using ;; const token = { cancelButtononclick = cancel;}; // Write your asynchronous code:const promise = token $tokenthendoSomething $tokenthendoSomethingElse $tokencatchhandleError; // If you cancel, the "promise" object will reject with an Cancelled error.
Documentation
This library is still experimental and may change in future releases.
CancelToken
The CancelToken
object is used to represent a cancellable operation.
Constructor: new CancelToken([callback] [, ...parentTokens])
- callback: A function that get the
cancel
function as a parameter. - parentTokens: Tokens that will propagate their cancelled state to this token.
token.chain:
An object providing several utility functions to chain promises in a cancellable way.
token.chain.then: (alias: token.then) promise::token.chain.then(onFulfilled[, onRejected])
Similar to Promise.prototype.then
. If the token is in a cancelled state, onFulfilled
and onRejected
will not be called, and the returned promise will reject with the Cancelled error.
token.chain.catch: (alias: token.catch) promise::token.chain.catch(onRejected)
Similar to Promise.prototype.catch
. If the token is in a cancelled state, onRejected
will not be called, and the returned promise will reject with the Cancelled error.
token.newPromise: token.newPromise((resolve, reject) => {})
A Promise factory, that returns a rejected Promise if the token is cancelled, or construct a new Promise. The callback is not called is the token is cancelled.
token.resolve: token.resolve(value)
A function that returns a rejected Promise if the token is cancelled, or a Promise resolved with the given value.
token.reject: token.reject(value)
A function that returns a rejected Promise if the token is cancelled, or a Promise rejected with the given value.
Cancelled
An Cancelled
is used to represent the cancellation of an operation. It is the rejected value to propagate the cancellation through a chain of promises.
Utility functions
always: (aliases: token.chain.always, token.always) promise::always(callback)
Use always
to always call a callback in a chain of promises. The returned or thrown value
Other Examples
Cancellable Request
const request = { const token = cancelToken; let cancelListener; return { const xhr = ; { xhr; ; }; token; xhr; xhr { if thisstatus >= 200 && thisstatus < 300 ; else ; }; xhr { ; }; xhr; }::};
Cancel Previous Operations
let previousCancel = null; { let clean = null; if previousCancel ; const token = { previousCancel = cancel; { if previousCancel === cancel previousCancel = null; }; }; ::thistokenchain ::;}
Cancel Operations When Removing a Widget
{ thistoken = { thiscancel = cancel; }; } { this; } { ::thistokenchain; } // other methods ...
Cancellable setTimeout
const setCancellableTimeout = { if !tokenaborted let id = 0; const cancel = { ; token; }; id = ; };