complete future only once in or (fixes #8982) (#9632)

* complete future only once in `or`

Analogous to `and`.
Credits to @k0zmo for proposing the solution.

* add test
This commit is contained in:
Miran 2018-11-09 23:45:17 +01:00 • committed by Dominik Picheta
commit 964b5dac7c
2 changed files with 36 additions and 2 deletions

View file

@ -375,8 +375,9 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
## complete.
var retFuture = newFuture[void]("asyncdispatch.`or`")
proc cb[X](fut: Future[X]) =
if fut.failed: retFuture.fail(fut.error)
if not retFuture.finished: retFuture.complete()
if not retFuture.finished:
if fut.failed: retFuture.fail(fut.error)
else: retFuture.complete()
fut1.callback = cb[T]
fut2.callback = cb[Y]
return retFuture