libutil: add asyncJoin, a Result-based joinPromises

we'll need this to asyncify withFramedSink and remove its thread pool.

Change-Id: I1a099392c094f8441482fde3b2d3843931420ffa
This commit is contained in:
eldritch horrors
2025-06-17 14:34:05 +02:00
parent 5f42f66afa
commit ba2432f8fe
+19
View File
@@ -136,4 +136,23 @@ kj::Promise<Result<void>> asyncSpread(Input && input, Fn fn)
co_return result::success();
}
/**
* Run `promises` concurrently until they've all succeeded, or pass on
* the error returned by the first failing promise. This is similar to
* `kj::joinPromisesFailFast()`, but not limited to thrown exceptions.
*/
template<typename... Promises>
kj::Promise<Result<void>> asyncJoin(Promises &&... promises)
requires(std::same_as<Promises, kj::Promise<Result<void>>> && ...)
{
auto collect =
asyncCollect(kj::arr(std::pair{std::tuple{}, std::forward<Promises>(promises)}...));
while (auto r = co_await collect.next()) {
if (!r->second.has_value()) {
co_return std::move(r->second);
}
}
co_return result::success();
}
}