CancelablePromise
title: CancelablePromise
Classes
CancelablePromise ⇐ Class
Kind: global class
Extends: Class
- CancelablePromise ⇐
Class
CancelablePromise(executor)
Cancelable promise
Important This is NOT an attempt to poly-fill the future standard of cancelable promise.
CancelablePromise is a Promises/A+ promise implementation, that offers a mean to cancel a promise.
A canceled promise is a promise that is rejected (to avoid memory leak, when a promise is canceled they must be settled).
But, the chain of callbacks set with then
or catch
are not called whenever the promise has been canceled.
**EXPERIMENTAL Do not use CancelablePromise if you don’t really need it **
- A lot of use cases are not tested
all
andrace
are not testedonCancel
is experimental
Param | Type | Description |
---|---|---|
executor | function | A function that is passed with the arguments resolve and reject. |
Example
var myPromise = new $CancelablePromise(function(resolve) { setTimeout(function() {resolve("I'm resolved");}, 100)});
myPromise.then(function(response) { console.log(response); }).then(function() {console.log("not cancel")});
myPromise.cancel();
// Nothing will be displayed in console
CancelablePromise(executor)
Create a promise that can be cancelled.
Param | Type |
---|---|
executor | function |
then(onFulfilled, onRejected) ⇒ CancelablePromise
The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. It returns a CancelablePromise which is determined by the the input functions:
- If onFulfilled or onRejected throws an error, or returns a Promise which rejects, then returns a rejected Promise.
- If onFulfilled or onRejected returns a Promise which resolves, or returns any other value, then returns a resolved Promise.
Kind: instance method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise
Param | Type | Description |
---|---|---|
onFulfilled | function | null | A Function called when the Promise is fulfilled. This function has one argument, the fulfillment value. |
onRejected | function | null | A Function called when the Promise is rejected. This function has one argument, the rejection reason. |
catch(onRejected) ⇒ CancelablePromise
Returns a Promise and deals with rejected cases only.
It behaves the same as calling then(undefined, onRejected)
Kind: instance method of CancelablePromise
Param | Type | Description |
---|---|---|
onRejected | function | null | A Function called when the Promise is rejected. This function has one argument, the rejection reason. |
cancel()
Cancel the promise
Reject the promise with a CanceledError.
Whenever the promise is canceled, the callbaks of the then
/catch
chain won’t be called.
Kind: instance method of CancelablePromise
onCancel()
Called whenever the promise has been canceled Useful if you need to cancel ongoing tasks, such as xhr request, …
Kind: instance method of CancelablePromise
all(iterable) ⇒ CancelablePromise
Returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that resolves when all of the promises in the given iterable have resolved, or rejects if any of the promises rejects.
Param | Type | Description |
---|---|---|
iterable | Array | An iterable object, such as an Array |
race(iterable) ⇒ CancelablePromise
Returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that resolves or rejects as soon as one of the promises in the given iterable resolves or rejects.
Param | Type | Description |
---|---|---|
iterable | Array | An iterable object, such as an Array |
reject(reason) ⇒ CancelablePromise
Returns a Promise object that is rejected with the given reason.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that is rejected with the given reason.
Param | Type | Description |
---|---|---|
reason | Object | Reason why this Promise rejected. |
resolve(value) ⇒ CancelablePromise
Returns a CancelablePromise that is resolved with the given value.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that is resolved with the given value.
Param | Type | Description |
---|---|---|
value | Object | Argument to be resolved by this Promise. Can also be a Promise or a thenable to resolve. |
CancelablePromise
Kind: global class
CancelablePromise(executor)
Cancelable promise
Important This is NOT an attempt to poly-fill the future standard of cancelable promise.
CancelablePromise is a Promises/A+ promise implementation, that offers a mean to cancel a promise.
A canceled promise is a promise that is rejected (to avoid memory leak, when a promise is canceled they must be settled).
But, the chain of callbacks set with then
or catch
are not called whenever the promise has been canceled.
**EXPERIMENTAL Do not use CancelablePromise if you don’t really need it **
- A lot of use cases are not tested
all
andrace
are not testedonCancel
is experimental
Param | Type | Description |
---|---|---|
executor | function | A function that is passed with the arguments resolve and reject. |
Example
var myPromise = new $CancelablePromise(function(resolve) { setTimeout(function() {resolve("I'm resolved");}, 100)});
myPromise.then(function(response) { console.log(response); }).then(function() {console.log("not cancel")});
myPromise.cancel();
// Nothing will be displayed in console
CancelablePromise(executor)
Create a promise that can be cancelled.
Param | Type |
---|---|
executor | function |
then(onFulfilled, onRejected) ⇒ CancelablePromise
The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. It returns a CancelablePromise which is determined by the the input functions:
- If onFulfilled or onRejected throws an error, or returns a Promise which rejects, then returns a rejected Promise.
- If onFulfilled or onRejected returns a Promise which resolves, or returns any other value, then returns a resolved Promise.
Kind: instance method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise
Param | Type | Description |
---|---|---|
onFulfilled | function | null | A Function called when the Promise is fulfilled. This function has one argument, the fulfillment value. |
onRejected | function | null | A Function called when the Promise is rejected. This function has one argument, the rejection reason. |
catch(onRejected) ⇒ CancelablePromise
Returns a Promise and deals with rejected cases only.
It behaves the same as calling then(undefined, onRejected)
Kind: instance method of CancelablePromise
Param | Type | Description |
---|---|---|
onRejected | function | null | A Function called when the Promise is rejected. This function has one argument, the rejection reason. |
cancel()
Cancel the promise
Reject the promise with a CanceledError.
Whenever the promise is canceled, the callbaks of the then
/catch
chain won’t be called.
Kind: instance method of CancelablePromise
onCancel()
Called whenever the promise has been canceled Useful if you need to cancel ongoing tasks, such as xhr request, …
Kind: instance method of CancelablePromise
all(iterable) ⇒ CancelablePromise
Returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that resolves when all of the promises in the given iterable have resolved, or rejects if any of the promises rejects.
Param | Type | Description |
---|---|---|
iterable | Array | An iterable object, such as an Array |
race(iterable) ⇒ CancelablePromise
Returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that resolves or rejects as soon as one of the promises in the given iterable resolves or rejects.
Param | Type | Description |
---|---|---|
iterable | Array | An iterable object, such as an Array |
reject(reason) ⇒ CancelablePromise
Returns a Promise object that is rejected with the given reason.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that is rejected with the given reason.
Param | Type | Description |
---|---|---|
reason | Object | Reason why this Promise rejected. |
resolve(value) ⇒ CancelablePromise
Returns a CancelablePromise that is resolved with the given value.
Kind: static method of CancelablePromise
Returns: CancelablePromise
- A CancelablePromise that is resolved with the given value.
Param | Type | Description |
---|---|---|
value | Object | Argument to be resolved by this Promise. Can also be a Promise or a thenable to resolve. |