JavaScripture
Contribute via GitHub Feedback

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. ECMAScript 2017 introduced async function()s which return Promises and the await keyword which can simplify Promise based code.

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). See also the window unhandledrejecton event.

Example:

Run

Results:

 

finally(onFinally : Function) : Promise
onFinally() : undefined

Also schedules onFinally to be called when the promise has been either resolved or rejected.

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<Array>

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. See also allSettled().

Example:

Run

Results:

 

allSettled(promises : Iterable<Promise>) : Promise<Array>

Creates a new Promise that will be resolved when all of promises are resolved or rejected. The result of the promise is an Array containing objects with a status property (containing either 'fulfilled' or 'rejected') and either a value property (for fulfilled promises) or reason property (for rejected promises). See also all().

Example:

Run

Results:

 

any(promises : Iterable<Promise>) : Promise

Creates a new Promise that will be resolved when the first Promise it promises resolves. If all of promises are rejected, the returned promise will be rejected with an AggregateError containing all the rejected values. See also race().

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 promise is rejected before any resolve, the returned Promise will be rejected immediately and will provide the value of the Promise that was rejected. See also any().

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: