Unify async macro and futures for js and native targets
This commit is contained in:
parent
67ac1aef59
commit
c93655e8b4
7 changed files with 43 additions and 4 deletions
30
tests/async/tjsandnativeasync.nim
Normal file
30
tests/async/tjsandnativeasync.nim
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
discard """
|
||||
output: '''hi
|
||||
bye'''
|
||||
"""
|
||||
|
||||
import async, times
|
||||
when defined(js):
|
||||
proc sleepAsync(t: int): Future[void] =
|
||||
var promise = newPromise() do(resolve: proc()):
|
||||
{.emit: """
|
||||
setTimeout(function(){
|
||||
`resolve`();
|
||||
}, `t`);
|
||||
""".}
|
||||
result = promise
|
||||
else:
|
||||
from asyncdispatch import sleepAsync, waitFor
|
||||
|
||||
proc foo() {.async.} =
|
||||
echo "hi"
|
||||
var s = epochTime()
|
||||
await sleepAsync(500)
|
||||
var e = epochTime()
|
||||
doAssert(e - s > 0.1)
|
||||
echo "bye"
|
||||
|
||||
when defined(js):
|
||||
discard foo()
|
||||
else:
|
||||
waitFor foo()
|
||||
Loading…
Add table
Add a link
Reference in a new issue