mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-03-01 16:40:32 +00:00
chore: list raised exceptions in utils
module (#1252)
part of https://github.com/vacp2p/nim-libp2p/issues/962 effort.
This commit is contained in:
parent
37e0f61679
commit
a6e45d6157
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user