chore: list raised exceptions in utils module (#1252)

part of https://github.com/vacp2p/nim-libp2p/issues/962 effort.
This commit is contained in:
vladopajic 2025-02-13 16:27:29 +01:00 committed by GitHub
parent 37e0f61679
commit a6e45d6157
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,21 +13,25 @@ import chronos
type AllFuturesFailedError* = object of CatchableError
proc anyCompleted*[T](futs: seq[Future[T]]): Future[Future[T]] {.async.} =
proc anyCompleted*[T](
futs: seq[Future[T]]
): Future[Future[T]] {.async: (raises: [AllFuturesFailedError, CancelledError]).} =
## Returns a future that will complete with the first future that completes.
## If all futures fail or futs is empty, the returned future will fail with AllFuturesFailedError.
var requests = futs
while true:
if requests.len == 0:
var raceFut: Future[T]
try:
raceFut = await one(requests)
if raceFut.completed:
return raceFut
except ValueError:
raise newException(
AllFuturesFailedError, "None of the futures completed successfully"
)
var raceFut = await one(requests)
if raceFut.completed:
return raceFut
let index = requests.find(raceFut)
requests.del(index)