Promise : Object
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
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:
RunResults:
Instance Methods
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:
RunResults:
Also schedules onFinally
to be called when the promise has been either resolved or rejected.
Example:
RunResults:
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:
RunResults:
Promise Methods
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:
RunResults:
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:
RunResults:
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:
RunResults:
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.