|
@@ -25,21 +25,21 @@ package js;
|
|
|
import haxe.extern.EitherType;
|
|
|
|
|
|
/**
|
|
|
- The Promise object represents the eventual completion (or failure) of an
|
|
|
+ The Promise object represents the eventual completion (or failure) of an
|
|
|
asynchronous operation and its resulting value.
|
|
|
-
|
|
|
+
|
|
|
Documentation [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
|
|
|
**/
|
|
|
@:native("Promise")
|
|
|
extern class Promise<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
|
|
|
+ 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.
|
|
|
**/
|
|
|
@:overload(function<T>(promise : Promise<T>) : Promise<T> {})
|
|
@@ -52,20 +52,20 @@ extern class Promise<T>
|
|
|
static function reject<T>( ?reason : Dynamic ) : Promise<T>;
|
|
|
|
|
|
/**
|
|
|
- Returns a promise that either fulfills when all of the promises in the
|
|
|
+ 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
|
|
|
+ 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.
|
|
|
**/
|
|
|
static function all( iterable : Array<Dynamic> ) : Promise<Array<Dynamic>>;
|
|
|
|
|
|
/**
|
|
|
- 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
|
|
|
+ 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.
|
|
|
**/
|
|
|
static function race( iterable : Array<Dynamic> ) : Promise<Dynamic>;
|
|
@@ -74,26 +74,31 @@ extern class Promise<T>
|
|
|
function new( init : (resolve : (value : T) -> Void, reject: (reason : Dynamic) -> Void) -> Void ) : Void;
|
|
|
|
|
|
/**
|
|
|
- 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
|
|
|
+ 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).
|
|
|
**/
|
|
|
- function then<TOut>( fulfillCallback : Null<PromiseCallback<T, TOut>>, ?rejectCallback : EitherType<Dynamic -> Void, PromiseCallback<Dynamic, TOut>> ) : Promise<TOut>;
|
|
|
+ function then<TOut>( fulfillCallback : Null<PromiseHandler<T, TOut>>, ?rejectCallback : PromiseHandler<Dynamic, TOut> ) : Promise<TOut>;
|
|
|
|
|
|
/**
|
|
|
- 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,
|
|
|
+ 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.
|
|
|
**/
|
|
|
@:native("catch")
|
|
|
- function catchError<TOut>( rejectCallback : EitherType<Dynamic -> Void, PromiseCallback<Dynamic, TOut>> ) : Promise<TOut>;
|
|
|
+ function catchError<TOut>( rejectCallback : PromiseHandler<Dynamic, TOut> ) : Promise<TOut>;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- Callback for the Promise object.
|
|
|
+ Handler type for the Promise object.
|
|
|
**/
|
|
|
-typedef PromiseCallback<T, TOut> = EitherType<T -> TOut, T -> Promise<TOut>>;
|
|
|
+abstract PromiseHandler<T,TOut>(T->Dynamic) // T->Dynamic, so the compiler always knows the type of the argument and can infer it for then/catch callbacks
|
|
|
+ from T->Promise<TOut> // direct casts have priority, so we use that to prioritize the Promise<TOut> return type
|
|
|
+{
|
|
|
+ // function casts are checked after direct ones, so we place T->TOut here to make it have lower priority than T->Promise<TOut>
|
|
|
+ @:from static inline function fromNonPromise<T,TOut>(f:T->TOut):PromiseHandler<T,TOut> return cast f;
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
A value with a `then` method.
|