Class: Promise

Promise(action)

This class wraps asynchronous code with easy to follow then and catch lambdas. A function that returns a Promise can be used as fn().then(result => console.log(result)).catch(error => console.log("Error:" + error)); for example. Promises start as pending, and then either resolve with a value, reject with a reason, or take on the state of another Promise.

Constructor

new Promise(action)

Constructs a new Promise.
Parameters:
Name Type Description
action function This should be a call back that takes two parameters: a resolve callback and a reject callback. When the operation is successful, the resolve callback should be called with the result value and if there is an error, the reject callback should be called with an error message.

Methods

(static) all(promises) → {Promise}

Creates a Promise whose resolution is an Array of all the promises' resolution, and whose rejection is the first error to occur if any.
Parameters:
Name Type Description
promises * Any iterable such as an Array of Promise objects.
Returns:
A Promise whose resolution is an Array of all the promises' resolutions.
Type
Promise

(static) reject(reason) → {Promise}

Creates a Promise that rejects with a given reason.
Parameters:
Name Type Description
reason * Any reason
Returns:
A promise that rejects with the given reason.
Type
Promise

(static) resolve(value) → {Promise}

Creates a Promise that is resolved with a given value.
Parameters:
Name Type Description
value * Any value
Returns:
A promise that resolves to the given value.
Type
Promise

catch(callback) → {Promise}

Adds a callback that is called when the Promise rejects with an error. The callback should take a parameter that is the reason for the rejection.
Parameters:
Name Type Description
callback function A callback
Returns:
This Promise so that calls may be chained.
Type
Promise

finally(callback) → {Promise}

Adds a callback to be executed regardless if the Promise is resolved or rejected. The callback does not receive any parameters.
Parameters:
Name Type Description
callback function A callback with no parameters.
Returns:
This Promise
Type
Promise

then(onResolve, onRejectedopt) → {Promise}

Adds a callback to be called when the Promise is resolved and an optional callback to handle rejection.
Parameters:
Name Type Attributes Description
onResolve function A callback that receives the resolved value
onRejected function <optional>
An optional callback that receives a rejection reason
Returns:
This Promise so that calls may be chained.
Type
Promise