combineSync<T extends Object> function

Sync<List<T>> combineSync<T extends Object>(
  1. Iterable<Sync<T>> syncs, {
  2. @noFutures Err<List<T>> onErr(
    1. List<Result<T>> allResults
    )?,
})

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;
    }
  });
}