combineAsync<T extends Object> function
Combines an iterable of Asyncs into one containing a list of their values.
The inputs are awaited concurrently. If any resolves to an Err, applies
the onErr
function to combine errors.
Implementation
Async<List<T>> combineAsync<T extends Object>(
Iterable<Async<T>> asyncs, {
@noFutures Err<List<T>> Function(List<Result<T>> allResults)? onErr,
}) {
if (asyncs.isEmpty) {
return Async.okValue([]);
}
return Async(() async {
final results = await Future.wait(asyncs.map((a) => a.value));
final combined = combineResult(results, onErr: onErr);
switch (combined) {
case Ok(value: final value):
return value;
case Err err:
throw err;
}
});
}