combineSync<T extends Object> function
Combines an iterable of Syncs into one containing a list of their values.
If any Sync contains an Err, applies the onErr
function to combine
errors.
Implementation
Sync<List<T>> combineSync<T extends Object>(
Iterable<Sync<T>> syncs, {
@noFutures Err<List<T>> Function(List<Result<T>> allResults)? onErr,
}) {
if (syncs.isEmpty) {
return Sync.okValue([]);
}
return Sync(() {
final results = syncs.map((s) => s.value).toList();
final combined = combineResult(results, onErr: onErr);
switch (combined) {
case Ok(value: final value):
return value;
case Err err:
throw err;
}
});
}