Browse Source

[js] Use type parameters in all(), allSettled() and race() (#10247)

* [js] Use type parameters in all(), allSettled() and race()

* [js] add @:overloads for iterable: Array<Dynamic>

* [js] Promise iterable:Array<T>for the case where non-promise values are used

* [js] Promise remove overload made redundant by iterable:Array<Dynamic>
George Corney 4 years ago
parent
commit
b3419845d6
1 changed files with 8 additions and 5 deletions
  1. 8 5
      std/js/lib/Promise.hx

+ 8 - 5
std/js/lib/Promise.hx

@@ -59,7 +59,8 @@ extern class Promise<T> {
 		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>>;
+	@:overload(function(iterable:Array<Dynamic>):Promise<Array<Dynamic>> {})
+	static function all<T>(iterable:Array<Promise<T>>):Promise<Array<T>>;
 
 	/**
 		Returns a promise that resolves after all of the given promises have either fulfilled or rejected,
@@ -71,14 +72,16 @@ extern class Promise<T> {
 		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.
 	**/
-	static function allSettled(iterable:Array<Dynamic>):Promise<Array<PromiseSettleOutcome>>;
+	@:overload(function(iterable:Array<Dynamic>):Promise<Array<PromiseSettleOutcome<Dynamic>>> {})
+	static function allSettled<T>(iterable:Array<Promise<T>>):Promise<Array<PromiseSettleOutcome<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.
 	**/
-	static function race(iterable:Array<Dynamic>):Promise<Dynamic>;
+	@:overload(function(iterable:Array<Dynamic>):Promise<Dynamic> {})
+	static function race<T>(iterable:Array<Promise<T>>):Promise<T>;
 
 	/** @throws DOMError */
 	function new(init:(resolve:(value:T) -> Void, reject:(reason:Dynamic) -> Void) -> Void):Void;
@@ -129,9 +132,9 @@ typedef ThenableStruct<T> = {
 	function then<TOut>(onFulfilled:Null<PromiseHandler<T, TOut>>, ?onRejected:PromiseHandler<Dynamic, TOut>):Thenable<TOut>;
 }
 
-typedef PromiseSettleOutcome = {
+typedef PromiseSettleOutcome<T> = {
 	var status:PromiseSettleStatus;
-	var ?value:Dynamic;
+	var ?value:T;
 	var ?reason:Dynamic;
 }