Promise

js.lib.Promise (extern class)

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

Documentation Promise by Mozilla Contributors, licensed under CC-BY-SA 2.5.

Static Members

resolve(thenable: Thenable<resolve.T>): Promise<resolve.T>

Returns a Promise object that is resolved with the given value. If the value is Thenable, the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. Generally, when it's unknown when value is a promise or not, use Promise.resolve(value) instead and work with the return value as a promise.

Name Type
thenable Thenable<resolve.T>
Returns
Promise<resolve.T>

reject(?reason: Dynamic): Promise<reject.T>

Returns a Promise object that is rejected with the given reason.

Name Type Default
reason Dynamic (optional)
Returns
Promise<reject.T>

all(iterable: Array<Promise<all.T>>): Promise<Array<all.T>>

Returns a promise that either fulfills when all of the promises in the iterable argument have fulfilled or rejects as soon as one of the promises in the iterable argument rejects. If the returned promise fulfills, it is fulfilled with an array of the values from the fulfilled promises in the same order as defined in the iterable. If the returned promise rejects, it is rejected with the reason from the first promise in the iterable that rejected. This method can be useful for aggregating results of multiple promises.

Name Type
iterable Array<Promise<all.T>>
Returns
Promise<Array<all.T>>

allSettled(iterable: Array<Promise<allSettled.T>>): Promise<Array<PromiseSettleOutcome>>

Returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.

It is typically used when you have multiple asynchronous tasks that are not dependent on one another to complete successfully, or you'd always like to know the result of each promise.

In comparison, the Promise returned by Promise.all may be more appropriate if the tasks are dependent on each other / if you'd like to immediately reject upon any of them rejecting.

Name Type
iterable Array<Promise<allSettled.T>>
Returns
Promise<Array<PromiseSettleOutcome>>

race(iterable: Array<Promise<race.T>>): Promise<race.T>

Returns a promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects, with the value or reason from that promise.

Name Type
iterable Array<Promise<race.T>>
Returns
Promise<race.T>

Instance Members

then(onFulfilled: Null<PromiseHandler<js.lib.Promise.T, then.TOut>>, ?onRejected: PromiseHandler<Dynamic, then.TOut>): Promise<then.TOut>

Appends fulfillment and rejection handlers to the promise and returns a new promise resolving to the return value of the called handler, or to its original settled value if the promise was not handled (i.e. if the relevant handler onFulfilled or onRejected is not a function).

Name Type Default
onFulfilled Null<PromiseHandler<js.lib.Promise.T, then.TOut>>
onRejected PromiseHandler<Dynamic, then.TOut> (optional)
Returns
Promise<then.TOut>

catchError(onRejected: PromiseHandler<Dynamic, js.lib.Promise.T>): Promise<js.lib.Promise.T>

Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.

Name Type
onRejected PromiseHandler<Dynamic, js.lib.Promise.T>
Returns
Promise<js.lib.Promise.T>

finally(onFinally: Function): Promise<js.lib.Promise.T>

Returns a Promise. When the promise is settled, i.e either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.

Name Type
onFinally Function
Returns
Promise<js.lib.Promise.T>

new(init: Function): Void
Name Type
init Function