summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-11-03 10:54:19 +0100
committerGitHub <noreply@github.com>2016-11-03 10:54:19 +0100
commitee8c1c6f93d6fb47719ec2f6390fc243c1bc8adc (patch)
tree14c972c519eedb253cd817b724334cdc0aaf15ec
parente784fdee4576ced94758ee64f20bd53d1abe2f6e (diff)
parentf2bded180f1c5fcb0994bc7d86d6221cd9ba6504 (diff)
downloadNim-ee8c1c6f93d6fb47719ec2f6390fc243c1bc8adc.tar.gz
Merge pull request #4989 from endragor/empty-async-all
async all() now immediately completes if arg is empty
-rw-r--r--lib/pure/includes/asyncfutures.nim7
-rw-r--r--tests/async/tasyncall.nim9
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/pure/includes/asyncfutures.nim b/lib/pure/includes/asyncfutures.nim
index d78464a91..dfcfa37a0 100644
--- a/lib/pure/includes/asyncfutures.nim
+++ b/lib/pure/includes/asyncfutures.nim
@@ -246,6 +246,7 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
 proc all*[T](futs: varargs[Future[T]]): auto =
   ## Returns a future which will complete once
   ## 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
   ## 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:
             retFuture.complete()
 
+    if totalFutures == 0:
+      retFuture.complete()
+
     return retFuture
 
   else:
@@ -292,4 +296,7 @@ proc all*[T](futs: varargs[Future[T]]): auto =
 
       setCallback(i)
 
+    if retValues.len == 0:
+      retFuture.complete(retValues)
+
     return retFuture
diff --git a/tests/async/tasyncall.nim b/tests/async/tasyncall.nim
index 60ba557cc..63b2945a6 100644
--- a/tests/async/tasyncall.nim
+++ b/tests/async/tasyncall.nim
@@ -66,3 +66,12 @@ block:
 
   doAssert execTime * 100 < taskCount * sleepDuration
   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() == @[]