diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2018-01-10 23:11:26 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-10 23:11:26 +0000 |
commit | 2c905f5e81f29d79b26df966a413f01306775800 (patch) | |
tree | b26356fa5c3561cefd808ac0d35b2fb4c1ec07d9 | |
parent | 1256f211f5c1d40b477baa70f70b7e4fe2a289a8 (diff) | |
parent | 52cc925e0e34580403b1a405ec8b4fa44c844de5 (diff) | |
download | Nim-2c905f5e81f29d79b26df966a413f01306775800.tar.gz |
Merge pull request #6962 from nim-lang/fixes/6100
Fixes #6100.
-rw-r--r-- | lib/pure/asyncfutures.nim | 6 | ||||
-rw-r--r-- | lib/pure/asyncmacro.nim | 6 | ||||
-rw-r--r-- | tests/async/t6100.nim | 15 | ||||
-rw-r--r-- | tests/async/tfuturestream.nim | 2 |
4 files changed, 25 insertions, 4 deletions
diff --git a/lib/pure/asyncfutures.nim b/lib/pure/asyncfutures.nim index bcc3ab613..11461d994 100644 --- a/lib/pure/asyncfutures.nim +++ b/lib/pure/asyncfutures.nim @@ -324,12 +324,12 @@ proc mget*[T](future: FutureVar[T]): var T = ## Future has not been finished. result = Future[T](future).value -proc finished*[T](future: Future[T] | FutureVar[T]): bool = +proc finished*(future: FutureBase | FutureVar): bool = ## Determines whether ``future`` has completed. ## ## ``True`` may indicate an error or a value. Use ``failed`` to distinguish. - when future is FutureVar[T]: - result = (Future[T](future)).finished + when future is FutureVar: + result = (FutureBase(future)).finished else: result = future.finished diff --git a/lib/pure/asyncmacro.nim b/lib/pure/asyncmacro.nim index 8c679929d..f70714309 100644 --- a/lib/pure/asyncmacro.nim +++ b/lib/pure/asyncmacro.nim @@ -32,6 +32,12 @@ template createCb(retFutureSym, iteratorNameSym, try: if not nameIterVar.finished: var next = nameIterVar() + # Continue while the yielded future is already finished. + while (not next.isNil) and next.finished: + next = nameIterVar() + if nameIterVar.finished: + break + if next == nil: if not retFutureSym.finished: let msg = "Async procedure ($1) yielded `nil`, are you await'ing a " & diff --git a/tests/async/t6100.nim b/tests/async/t6100.nim new file mode 100644 index 000000000..b4dc0f146 --- /dev/null +++ b/tests/async/t6100.nim @@ -0,0 +1,15 @@ +discard """ + file: "t6100.nim" + exitcode: 0 + output: "10000000" +""" +import asyncdispatch + +let done = newFuture[int]() +done.complete(1) + +proc asyncSum: Future[int] {.async.} = + for _ in 1..10_000_000: + result += await done + +echo waitFor asyncSum() \ No newline at end of file diff --git a/tests/async/tfuturestream.nim b/tests/async/tfuturestream.nim index 9a8e986a0..d76752b7e 100644 --- a/tests/async/tfuturestream.nim +++ b/tests/async/tfuturestream.nim @@ -18,8 +18,8 @@ var fs = newFutureStream[int]() proc alpha() {.async.} = for i in 0 .. 5: - await sleepAsync(1000) await fs.write(i) + await sleepAsync(1000) echo("Done") fs.complete() |