Unify async macro and futures for js and native targets

This commit is contained in:
Yuriy Glukhov 2018-01-03 21:47:59 +02:00
commit c93655e8b4
7 changed files with 43 additions and 4 deletions

View file

@ -1,2 +0,0 @@
when defined(upcoming):
patchFile("stdlib", "asyncdispatch", "$lib/upcoming/asyncdispatch")

View 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()

View file

@ -1,5 +1,4 @@
discard """
disabled: true
output: '''
x
e

View file

@ -225,7 +225,8 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
"actiontable/tactiontable", "method/tmultim1",
"method/tmultim3", "method/tmultim4",
"varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
"varres/tvartup", "misc/tints", "misc/tunsignedinc"]:
"varres/tvartup", "misc/tints", "misc/tunsignedinc",
"async/tjsandnativeasync"]:
test "tests/" & testfile & ".nim"
for testfile in ["strutils", "json", "random", "times", "logging"]: