JavaScripture
Contribute via GitHub Feedback

JavaScript Promise : Object

A Promise is an object that represents an asynchronous operation that will eventually produce a value. Use the then() method to hook up a callback that will be called when the result of the asynchronous operation is ready.

Constructors

new Promise(executor : Function) : Promise
executor(resolve : Function, reject : Function) : undefined
resolve(value : Object) : undefined
reject(error : Error) : undefined

Creates a new Promise. The Promise constructor calls executer immediately with the two functions resolve and reject. executor should begin the asynchronous operation. When the operation is complete, call the resolve function with the result. If there is an error during the operation, call reject with the error information.

Example:

Run

Results:

 

Instance Methods

catch(onReject : Function) : Promise
onReject(error : Error) : undefined

Schedules onReject to be called if the promise had an error (ie, the executer function called reject() or a method in the promise chain threw an error). error is the value passed to reject. This is a shorthand for calling then(undefined, onReject).

Example:

Run

Results:

 

then(onResolve : Function, [onReject : Function]) : Promise
onResolve(value : Object) : Object
onReject(error : Error) : Object

Schedules onResolve (if provided) to be called when the promise has been resolved. value is the object passed to the resolve() function.

Also schedules onReject (if provided) to be called when the promise has been rejected or an exception was thrown in the executor method. error is the object passed to the reject() function.

The return value from onResolve (or onReject) can either be a normal Object or a Promise. If the return value from onResolve is a normal Object, the Promise returned by then() will be resolved with that Object. If the return value from onResolve is a Promise, then() will wait for that Promise to be resolved. then() will resolve the Promise it returned with the same value.

See also catch().

Example:

Run

Results:

 

Promise Methods

all(promises : Iterable<Promise>) : Promise

Creates a new Promise that will be resolved when all of promises are resolved. If any of the promises are rejected, the returned Promise will be rejected immediately and will provide the value of the Promise that was rejected.

Example:

Run

Results:

 

race(promises : Iterable<Promise>) : Promise

Creates a new Promise that will be resolved when the first of promises is resolved. If a promises is rejected before any resolve, the returned Promise will be rejected immediately and will provide the value of the Promise that was rejected.

Example:

Run

Results:

 

reject(error : Error) : Promise

Returns a new Promise that is in the rejected state with error as the rejected error. Useful for passing values to APIs that expect promises.

Example:

Run

Results:

 

resolve(value : Object) : Promise

Returns a new Promise that is in the resolved state with value as the resolved value. Useful for passing values to APIs that expect promises.

Example:

Run

Results: