Merge pull request #4989 from endragor/empty-async-all
async all() now immediately completes if arg is empty
This commit is contained in:
commit
ee8c1c6f93
2 changed files with 16 additions and 0 deletions
|
|
@ -246,6 +246,7 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
|
||||||
proc all*[T](futs: varargs[Future[T]]): auto =
|
proc all*[T](futs: varargs[Future[T]]): auto =
|
||||||
## Returns a future which will complete once
|
## Returns a future which will complete once
|
||||||
## all futures in ``futs`` complete.
|
## all futures in ``futs`` complete.
|
||||||
|
## If the argument is empty, the returned future completes immediately.
|
||||||
##
|
##
|
||||||
## If the awaited futures are not ``Future[void]``, the returned future
|
## If the awaited futures are not ``Future[void]``, the returned future
|
||||||
## will hold the values of all awaited futures in a sequence.
|
## will hold the values of all awaited futures in a sequence.
|
||||||
|
|
@ -270,6 +271,9 @@ proc all*[T](futs: varargs[Future[T]]): auto =
|
||||||
if completedFutures == totalFutures:
|
if completedFutures == totalFutures:
|
||||||
retFuture.complete()
|
retFuture.complete()
|
||||||
|
|
||||||
|
if totalFutures == 0:
|
||||||
|
retFuture.complete()
|
||||||
|
|
||||||
return retFuture
|
return retFuture
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -292,4 +296,7 @@ proc all*[T](futs: varargs[Future[T]]): auto =
|
||||||
|
|
||||||
setCallback(i)
|
setCallback(i)
|
||||||
|
|
||||||
|
if retValues.len == 0:
|
||||||
|
retFuture.complete(retValues)
|
||||||
|
|
||||||
return retFuture
|
return retFuture
|
||||||
|
|
|
||||||
|
|
@ -66,3 +66,12 @@ block:
|
||||||
|
|
||||||
doAssert execTime * 100 < taskCount * sleepDuration
|
doAssert execTime * 100 < taskCount * sleepDuration
|
||||||
doAssert results == expected
|
doAssert results == expected
|
||||||
|
|
||||||
|
block:
|
||||||
|
let
|
||||||
|
noIntFuturesFut = all(newSeq[Future[int]]())
|
||||||
|
noVoidFuturesFut = all(newSeq[Future[void]]())
|
||||||
|
|
||||||
|
doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed
|
||||||
|
doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed
|
||||||
|
doAssert noIntFuturesFut.read() == @[]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue