captureAll<T> static method
Captures each future in elements
,
Returns a (future of) a list of results for each element in elements
,
in iteration order.
Each future in elements
is captured and each non-future is
wrapped as a Result.value.
The returned future will never have an error.
Implementation
static Future<List<Result<T>>> captureAll<T>(Iterable<FutureOr<T>> elements) {
var results = <Result<T>?>[];
var pending = 0;
late Completer<List<Result<T>>> completer;
for (var element in elements) {
if (element is Future<T>) {
var i = results.length;
results.add(null);
pending++;
Result.capture<T>(element).then((result) {
results[i] = result;
if (--pending == 0) {
completer.complete(List.from(results));
}
});
} else {
results.add(Result<T>.value(element));
}
}
if (pending == 0) {
return Future.value(List.from(results));
}
completer = Completer<List<Result<T>>>();
return completer.future;
}