Recommend this page to a friend! |
Download |
Info | Documentation | Demos | Files | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 42 | All time: 509 This week: 1 |
Version | License | JavaScript version | Categories | |||
cancellable-chain-of 1.0 | MIT/X Consortium ... | 6 | Language, EcmaScript 6 |
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.
This library relies on the Cancellation proposal, and the prex implementation.
import { CancellationTokenSource } from 'prex';
import makeChain from 'cancellable-chain-of-promises';
const source = new CancellationTokenSource();
cancelButton.onclick = () => source.cancel();
// Write your asynchronous code:
const chain = makeChain(source.token);
const promise = chain.resolve()
::chain.then(doSomething)
::chain.then(doSomethingElse)
::chain.catch(handleError);
// If you cancel, the "promise" object will reject with an Cancelled error.
like-bind-operator
import { CancellationTokenSource } from 'prex';
import makeChain from 'cancellable-chain-of-promises';
import $ from 'like-bind-operator';
const source = new CancellationTokenSource();
cancelButton.onclick = () => source.cancel();
// Write your asynchronous code:
const chain = makeChain(source.token);
const promise = chain.resolve()
$(doSomething)
$(doSomethingElse)
$(handleError);
// If you cancel, the "promise" object will reject with an Cancelled error.
This library is still experimental and may change in future releases.
The chain
object contains.
makeChain: chain = makeChain(source.token)
Creates a chain object linked to a cancellation token.
chain.then: promise::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.
chain.catch: promise::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.
chain.newPromise: chain.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.
chain.resolve: chain.resolve(value)
A function that returns a rejected Promise if the token is cancelled, or a Promise resolved with the given value.
chain.reject: chain.reject(value)
A function that returns a rejected Promise if the token is cancelled, or a Promise rejected with the given value.
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.
always: (aliases: chain.always) promise::always(callback)
Use always
to always call a callback in a chain of promises. The returned or thrown value
Check some examples in src/utils/
.
const request = (url, { method, body, cancelToken = CancellationToken.none }) => {
let registration;
return new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
registration = cancelToken.register(() => {
xhr.abort();
reject(new Cancelled(cancelToken));
});
xhr.open(method, url);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send(body);
})::always(() => registration.unregister())
};
class Widget {
constructor() {
this.source = new CancellationTokenSource();
this.chain = makeChain(this.source.token);
}
destroy() {
this.source.cancel();
}
onclick() {
request('/random', {cancelToken: this.source.token})
::this.chain.then(response => this.updateState(response));
}
// other methods ...
}
Files (15) |
File | Role | Description | ||
---|---|---|---|---|
src (1 file, 1 directory) | ||||
test (3 files) | ||||
.babelrc | Data | Auxiliary data | ||
.editorconfig | Data | Auxiliary data | ||
.eslintignore | Data | Auxiliary data | ||
.eslintrc.json | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
package.json | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (15) | / | src | / | utils |
File | Role | Description |
---|---|---|
cancelPreviousCalls.js | Example | Example script |
setCancellableInterval.js | Aux. | Auxiliary script |
setCancellableTimeout.js | Aux. | Auxiliary script |
Files (15) | / | test |
File | Role | Description |
---|---|---|
.babelrc | Data | Auxiliary data |
test.js | Example | Example script |
testUtils.js | Example | Example script |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Related pages |
GitHub |
NPM Package |