@@ -40,21 +40,22 @@ class Promises {
40
40
* @param futures A list of Futures to resolve.
41
41
* @return A Future for a list of result values.
42
42
*/
43
- public static function allSettled <T >(futures : Array <Future <T >>): Future <Array <PromiseResult <T >>> {
44
- var promise : Promise <Array <PromiseResult <T >>> = new Promise <Array <PromiseResult <T >>>();
45
- var results : Array <PromiseResult <T >> = [];
43
+ public static function allSettled <T >(futures : Array <Future <T >>): Future <Array <Future <T >>> {
44
+ var promise : Promise <Array <Future <T >>> = new Promise <Array <Future <T >>>();
45
+
46
+ var resolved : Int = 0 ;
46
47
47
48
for (future in futures ) {
48
49
future .onComplete (function (value ) {
49
- results . push ( PromiseResult . fulfilled ( value )) ;
50
- if (results . length == futures .length ) {
51
- promise .complete (results );
50
+ resolved + = 1 ;
51
+ if (resolved == futures .length ) {
52
+ promise .complete (futures );
52
53
}
53
54
});
54
55
future .onError (function (error ) {
55
- results . push ( PromiseResult . rejected ( value )) ;
56
- if (results . length == futures .length ) {
57
- promise .complete (results );
56
+ resolved + = 1 ;
57
+ if (resolved == futures .length ) {
58
+ promise .complete (futures );
58
59
}
59
60
});
60
61
}
@@ -93,64 +94,16 @@ class Promises {
93
94
* @param futures A list of Futures to resolve.
94
95
* @return A Future for a result value.
95
96
*/
96
- public static function race <T >(futures : Array <Future <T >>): Future <PromiseResult < T > > {
97
- var promise : Promise <PromiseResult < T >> = new Promise <PromiseResult < T > >();
97
+ public static function race <T >(futures : Array <Future <T >>): Future <T > {
98
+ var promise : Promise <T > = new Promise <T >();
98
99
for (future in futures ) {
99
100
future .onComplete (function (value ) {
100
- promise .complete (PromiseResult . fulfilled ( value ) );
101
+ promise .complete (value );
101
102
});
102
103
future .onError (function (error ) {
103
- promise .complete ( PromiseResult . rejected ( error ) );
104
+ promise .error ( error );
104
105
});
105
106
}
106
107
return promise .future ;
107
108
}
108
109
}
109
-
110
- class PromiseResult <T > {
111
- /**
112
- * The current state of the promise.
113
- */
114
- public var state : PromiseState ;
115
- /**
116
- * The value of the promise, if it resolved as Fulfilled.
117
- */
118
- public var value : Null <T >;
119
- /**
120
- * The error of the promise, if it resolved as Rejected.
121
- */
122
- public var error : Null <Dynamic >;
123
-
124
- private function new (state : PromiseState , value : Null <T >, error : Null <Dynamic >): Void {
125
- this .state = state ;
126
- this .value = value ;
127
- this .error = error ;
128
- }
129
-
130
- public static function pending <T >(): PromiseResult <T > {
131
- return new PromiseResult <T >(PromiseState . Pending , null , null );
132
- }
133
-
134
- public static function fulfilled <T >(value : T ): PromiseResult <T > {
135
- return new PromiseResult <T >(PromiseState . Fulfilled , value , null );
136
- }
137
-
138
- public static function rejected <T >(error : Dynamic ): PromiseResult <T > {
139
- return new PromiseResult <T >(PromiseState . Rejected , null , error );
140
- }
141
- }
142
-
143
- enum PromiseState {
144
- /**
145
- * This promise has not yet resolved.
146
- */
147
- Pending ;
148
- /**
149
- * This promise has resolved with a value.
150
- */
151
- Fulfilled ;
152
- /**
153
- * This promise has resolved with an error.
154
- */
155
- Rejected ;
156
- }
0 commit comments