Skip to content

Commit

Permalink
Merge branch 'master' into raises-servces
Browse files Browse the repository at this point in the history
  • Loading branch information
vladopajic authored Feb 13, 2025
2 parents 0b53b9d + a6e45d6 commit b88e3ac
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libp2p/utils/future.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b88e3ac

Please sign in to comment.