Merge pull request #7023 from yglukhov/unify-async
Unify async macro and futures for js and native targets
This commit is contained in:
commit
b754bfabb6
7 changed files with 43 additions and 4 deletions
|
|
@ -24,6 +24,7 @@ environment:
|
||||||
# platform: x86
|
# platform: x86
|
||||||
|
|
||||||
install:
|
install:
|
||||||
|
- ps: Install-Product node 8 # node 8 or later is required to test js async stuff
|
||||||
- MKDIR %CD%\DIST
|
- MKDIR %CD%\DIST
|
||||||
- MKDIR %CD%\DIST\PCRE
|
- MKDIR %CD%\DIST\PCRE
|
||||||
- nuget install pcre -Verbosity quiet -Version 8.33.0.1 -OutputDirectory %CD%\DIST\PCRE
|
- nuget install pcre -Verbosity quiet -Version 8.33.0.1 -OutputDirectory %CD%\DIST\PCRE
|
||||||
|
|
|
||||||
|
|
@ -139,3 +139,7 @@ macro async*(arg: untyped): untyped =
|
||||||
proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importcpp: "(new Promise(#))".}
|
proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importcpp: "(new Promise(#))".}
|
||||||
## A helper for wrapping callback-based functions
|
## A helper for wrapping callback-based functions
|
||||||
## into promises and async procedures
|
## into promises and async procedures
|
||||||
|
|
||||||
|
proc newPromise*(handler: proc(resolve: proc())): Future[void] {.importcpp: "(new Promise(#))".}
|
||||||
|
## A helper for wrapping callback-based functions
|
||||||
|
## into promises and async procedures
|
||||||
|
|
|
||||||
6
lib/pure/async.nim
Normal file
6
lib/pure/async.nim
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
when defined(js):
|
||||||
|
import asyncjs
|
||||||
|
export asyncjs
|
||||||
|
else:
|
||||||
|
import asyncmacro, asyncfutures
|
||||||
|
export asyncmacro, asyncfutures
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
when defined(upcoming):
|
|
||||||
patchFile("stdlib", "asyncdispatch", "$lib/upcoming/asyncdispatch")
|
|
||||||
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()
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
discard """
|
discard """
|
||||||
disabled: true
|
|
||||||
output: '''
|
output: '''
|
||||||
x
|
x
|
||||||
e
|
e
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,8 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
|
||||||
"actiontable/tactiontable", "method/tmultim1",
|
"actiontable/tactiontable", "method/tmultim1",
|
||||||
"method/tmultim3", "method/tmultim4",
|
"method/tmultim3", "method/tmultim4",
|
||||||
"varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
|
"varres/tvarres0", "varres/tvarres3", "varres/tvarres4",
|
||||||
"varres/tvartup", "misc/tints", "misc/tunsignedinc"]:
|
"varres/tvartup", "misc/tints", "misc/tunsignedinc",
|
||||||
|
"async/tjsandnativeasync"]:
|
||||||
test "tests/" & testfile & ".nim"
|
test "tests/" & testfile & ".nim"
|
||||||
|
|
||||||
for testfile in ["strutils", "json", "random", "times", "logging"]:
|
for testfile in ["strutils", "json", "random", "times", "logging"]:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue