Skip to content

Commit 4e365e9

Browse files
Rework promise handling to use futures.
1 parent e35f002 commit 4e365e9

File tree

1 file changed

+14
-61
lines changed

1 file changed

+14
-61
lines changed

src/lime/app/Promises.hx

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ class Promises {
4040
* @param futures A list of Futures to resolve.
4141
* @return A Future for a list of result values.
4242
*/
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;
4647

4748
for (future in futures) {
4849
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);
5253
}
5354
});
5455
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);
5859
}
5960
});
6061
}
@@ -93,64 +94,16 @@ class Promises {
9394
* @param futures A list of Futures to resolve.
9495
* @return A Future for a result value.
9596
*/
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>();
9899
for (future in futures) {
99100
future.onComplete(function(value) {
100-
promise.complete(PromiseResult.fulfilled(value));
101+
promise.complete(value);
101102
});
102103
future.onError(function(error) {
103-
promise.complete(PromiseResult.rejected(error));
104+
promise.error(error);
104105
});
105106
}
106107
return promise.future;
107108
}
108109
}
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

Comments
 (0)