summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2018-01-17 15:06:32 +0000
committerGitHub <noreply@github.com>2018-01-17 15:06:32 +0000
commitb754bfabb625d9ecac4939a156e0499e0f476370 (patch)
treea4aac963ba00a02e454c96a212df1f2adffda203
parent399c5e38b7bf7a0998ca9ed4bce57c7092d59229 (diff)
parentc93655e8b492dacbf35e5152e716035e112c899f (diff)
downloadNim-b754bfabb625d9ecac4939a156e0499e0f476370.tar.gz
Merge pull request #7023 from yglukhov/unify-async
Unify async macro and futures for js and native targets
-rw-r--r--appveyor.yml1
-rw-r--r--lib/js/asyncjs.nim4
-rw-r--r--lib/pure/async.nim6
-rw-r--r--tests/async/config.nims2
-rw-r--r--tests/async/tjsandnativeasync.nim30
-rw-r--r--tests/js/tasync.nim1
-rw-r--r--tests/testament/categories.nim3
7 files changed, 43 insertions, 4 deletions
diff --git a/appveyor.yml b/appveyor.yml
index be2cc50d3..a79d32e41 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -24,6 +24,7 @@ environment:
     #   platform: x86
 
 install:
+  - ps: Install-Product node 8 # node 8 or later is required to test js async stuff
   - MKDIR %CD%\DIST
   - MKDIR %CD%\DIST\PCRE
   - nuget install pcre -Verbosity quiet -Version 8.33.0.1 -OutputDirectory %CD%\DIST\PCRE
diff --git a/lib/js/asyncjs.nim b/lib/js/asyncjs.nim
index ec410ee39..3c488dcfe 100644
--- a/lib/js/asyncjs.nim
+++ b/lib/js/asyncjs.nim
@@ -139,3 +139,7 @@ macro async*(arg: untyped): untyped =
 proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importcpp: "(new Promise(#))".}
   ## A helper for wrapping callback-based functions
   ## 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
diff --git a/lib/pure/async.nim b/lib/pure/async.nim
new file mode 100644
index 000000000..97b29f81d
--- /dev/null
+++ b/lib/pure/async.nim
@@ -0,0 +1,6 @@
+when defined(js):
+    import asyncjs
+    export asyncjs
+else:
+    import asyncmacro, asyncfutures
+    export asyncmacro, asyncfutures
diff --git a/tests/async/config.nims b/tests/async/config.nims
deleted file mode 100644
index 97c2e0aa4..000000000
--- a/tests/async/config.nims
+++ /dev/null
@@ -1,2 +0,0 @@
-when defined(upcoming):
-  patchFile("stdlib", "asyncdispatch", "$lib/upcoming/asyncdispatch")
diff --git a/tests/async/tjsandnativeasync.nim b/tests/async/tjsandnativeasync.nim
new file mode 100644
index 000000000..45839899f
--- /dev/null
+++ b/tests/async/tjsandnativeasync.nim
@@ -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()
diff --git a/tests/js/tasync.nim b/tests/js/tasync.nim
index 34ef97b8b..76db38b27 100644
--- a/tests/js/tasync.nim
+++ b/tests/js/tasync.nim
@@ -1,5 +1,4 @@
 discard """
-  disabled: true
   output: '''
 x
 e
diff --git a/tests/testament/categories.nim b/tests/testament/categories.nim
index 5c845fc5c..90468e627 100644
--- a/tests/testament/categories.nim
+++ b/tests/testament/categories.nim
@@ -226,7 +226,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"]: