소스 검색

[js] add Promise.allSettled (closes #9982)

Dan Korostelev 4 년 전
부모
커밋
52038d7b53
1개의 변경된 파일23개의 추가작업 그리고 0개의 파일을 삭제
  1. 23 0
      std/js/lib/Promise.hx

+ 23 - 0
std/js/lib/Promise.hx

@@ -61,6 +61,18 @@ extern class Promise<T> {
 	**/
 	static function all(iterable:Array<Dynamic>):Promise<Array<Dynamic>>;
 
+	/**
+		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.
+	**/
+	static function allSettled(iterable:Array<Dynamic>):Promise<Array<PromiseSettleOutcome>>;
+
 	/**
 		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
@@ -116,3 +128,14 @@ abstract Thenable<T>(ThenableStruct<T>)
 typedef ThenableStruct<T> = {
 	function then<TOut>(onFulfilled:Null<PromiseHandler<T, TOut>>, ?onRejected:PromiseHandler<Dynamic, TOut>):Thenable<TOut>;
 }
+
+typedef PromiseSettleOutcome = {
+	var status:PromiseSettleStatus;
+	var ?value:Dynamic;
+	var ?reason:Dynamic;
+}
+
+enum abstract PromiseSettleStatus(String) to String {
+	var Fulfilled = "fulfilled";
+	var Rejected = "rejected";
+}