Skip to content

Commit ab8a273

Browse files
Merge remote-tracking branch 'origin/feature/promise-handlers-public' into dev-rebase
2 parents 9478698 + 4e365e9 commit ab8a273

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

src/lime/app/Promises.hx

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package lime.app;
2+
3+
import lime.app.Promise;
4+
import lime.app.Future;
5+
6+
/**
7+
* A set of static utility functions for working with Promises.
8+
* This includes functions which resolve groups of Futures.
9+
*/
10+
class Promises {
11+
/**
12+
* Creates a promise which fulfills when all the input promises resolve successfully,
13+
* with an array of the result values.
14+
* Rejects when any of the input promises reject, with the first rejection reason.
15+
* @param futures A list of Futures to resolve.
16+
* @return A Future for a list of result values.
17+
*/
18+
public static function all<T>(futures:Array<Future<T>>):Future<Array<T>> {
19+
var promise:Promise<Array<T>> = new Promise<Array<T>>();
20+
var results:Array<T> = [];
21+
22+
for (future in futures) {
23+
future.onComplete(function(result) {
24+
results.push(result);
25+
if (results.length == futures.length) {
26+
promise.complete(results);
27+
}
28+
});
29+
future.onError(function(error) {
30+
promise.error(error);
31+
});
32+
}
33+
34+
return promise.future;
35+
}
36+
37+
/**
38+
* Creates a promise which fulfills when all the input promises settle (whether success or failure).
39+
* Returns an array of objects that describe the outcome of each promise.
40+
* @param futures A list of Futures to resolve.
41+
* @return A Future for a list of result values.
42+
*/
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;
47+
48+
for (future in futures) {
49+
future.onComplete(function(value) {
50+
resolved += 1;
51+
if (resolved == futures.length) {
52+
promise.complete(futures);
53+
}
54+
});
55+
future.onError(function(error) {
56+
resolved += 1;
57+
if (resolved == futures.length) {
58+
promise.complete(futures);
59+
}
60+
});
61+
}
62+
63+
return promise.future;
64+
}
65+
66+
/**
67+
* Creates a promise which fulfills when any of the input promises resolve successfully.
68+
* Returns the first fulfilled promise. If all promises reject, the promise will be rejected with the list of rejection reasons.
69+
* @param futures A list of Futures to resolve.
70+
* @return A Future for a result value.
71+
*/
72+
public static function any<T>(futures:Array<Future<T>>):Future<T> {
73+
var promise:Promise<T> = new Promise<T>();
74+
var errors:Array<Dynamic> = [];
75+
76+
for (future in futures) {
77+
future.onComplete(function(value) {
78+
promise.complete(value);
79+
});
80+
future.onError(function(error) {
81+
errors.push(error);
82+
if (errors.length == futures.length) {
83+
promise.error(errors);
84+
}
85+
});
86+
}
87+
88+
return promise.future;
89+
}
90+
91+
/**
92+
* Creates a promise which fulfills when any of the input promises settle.
93+
* Returns an object that describes the outcome of the first settled promise (whether success or failure).
94+
* @param futures A list of Futures to resolve.
95+
* @return A Future for a result value.
96+
*/
97+
public static function race<T>(futures:Array<Future<T>>):Future<T> {
98+
var promise:Promise<T> = new Promise<T>();
99+
for (future in futures) {
100+
future.onComplete(function(value) {
101+
promise.complete(value);
102+
});
103+
future.onError(function(error) {
104+
promise.error(error);
105+
});
106+
}
107+
return promise.future;
108+
}
109+
}

0 commit comments

Comments
 (0)