Is it possible to modify:
The following snippet so as to print alternatively "A" and "B", like:
A, B, B, A, A, B, etc...
import asyncdispatch
proc A() {.async.} =
for _ in 1..10:
echo("A")
proc B() {.async.} =
for _ in 1..10:
echo("B")
await A()
await B()
It sounds like what you want is less "async output" and more "truly parallel execution with whatever work happens to finish earliest" winning the race. This is similar to the Unix process level wait4(-1,..) or waitpid(-1,..) which I have found quite useful from time to time, but on the finer work granularity of your echo example.
The Nim threadpool does have a blockUntilAny primitive with which you could build your sort of idea, but this issue I opened over 2 years ago still seems to fail: https://github.com/nim-lang/Nim/issues/8201
Yes, it is possible:
import asyncdispatch
proc A() {.async.} =
for _ in 1..10:
echo("A")
await sleepAsync(0)
proc B() {.async.} =
for _ in 1..10:
echo("B")
await sleepAsync(0)
asyncCheck A()
waitFor B()
I think waitfor A() and B() is cleaner ... that way you can handle any errors that occur.
I wouldn't say that it can handle errors, but doing it the way you've shown is indeed better as it ensures both A and B complete before the program exits.