Merge pull request #4241 from moigagoo/devel
Stdlib: asyncdispatch: Add proc `all`.
This commit is contained in:
commit
ef303e5a10
1 changed files with 23 additions and 1 deletions
|
|
@ -355,6 +355,28 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
|
||||||
fut2.callback = cb
|
fut2.callback = cb
|
||||||
return retFuture
|
return retFuture
|
||||||
|
|
||||||
|
proc all*[A](futs: seq[Future[A]]): Future[seq[A]] =
|
||||||
|
## Returns a future which will complete once all futures in ``futs``
|
||||||
|
## complete.
|
||||||
|
##
|
||||||
|
## The resulting future will hold the values of all awaited futures,
|
||||||
|
## in the order they are passed.
|
||||||
|
|
||||||
|
var
|
||||||
|
retFuture = newFuture[seq[A]]("asyncdispatch.all")
|
||||||
|
retValues = newSeq[A](len(futs))
|
||||||
|
completedFutures = 0
|
||||||
|
|
||||||
|
for i, fut in futs:
|
||||||
|
fut.callback = proc(f: Future[A]) =
|
||||||
|
retValues[i] = f.read()
|
||||||
|
inc(completedFutures)
|
||||||
|
|
||||||
|
if completedFutures == len(futs):
|
||||||
|
retFuture.complete(retValues)
|
||||||
|
|
||||||
|
return retFuture
|
||||||
|
|
||||||
type
|
type
|
||||||
PDispatcherBase = ref object of RootRef
|
PDispatcherBase = ref object of RootRef
|
||||||
timers: HeapQueue[tuple[finishAt: float, fut: Future[void]]]
|
timers: HeapQueue[tuple[finishAt: float, fut: Future[void]]]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue