parSequenceN<L, R> static method
TODO(parSequenceN)
Implementation
@experimental
static Future<Either<L, BuiltList<R>>> parSequenceN<L, R>(
Iterable<Future<Either<L, R>> Function()> functions,
int? n,
) async {
final futureFunctions = functions.toList(growable: false);
final semaphore = Semaphore(n ?? futureFunctions.length);
final token = _Token();
Future<R> Function() run(Future<Either<L, R>> Function() f) {
return () => Future.sync(f).then(
(e) => e.getOrHandle((l) => throw ControlError<L>._(l, token)),
);
}
Future<R> runWithPermit(Future<Either<L, R>> Function() f) =>
semaphore.withPermit(run(f));
return Future.wait(
futureFunctions.map(runWithPermit),
eagerError: true,
)
.then((values) => Either<L, BuiltList<R>>.right(values.build()))
.onError<ControlError<L>>(
(e, s) => Left(e._value),
test: (e) => identical(e._token, token),
);
}