mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-25 16:53:59 +00:00
asyncjs: add then, catch for promise pipelining (#16871)
* asyncjs: add then * improve tests, changelog, API * fix cryptic windows error: The parameter is incorrect * address comments
This commit is contained in:
@@ -23,3 +23,6 @@ hint("Processing", off)
|
||||
# switch("define", "nimTestsEnableFlaky")
|
||||
|
||||
# switch("hint", "ConvFromXtoItselfNotNeeded")
|
||||
|
||||
# experimental API's are enabled in testament, refs https://github.com/timotheecour/Nim/issues/575
|
||||
switch("define", "nimExperimentalAsyncjsThen")
|
||||
|
||||
@@ -2,32 +2,76 @@ discard """
|
||||
output: '''
|
||||
x
|
||||
e
|
||||
done
|
||||
'''
|
||||
"""
|
||||
|
||||
import asyncjs
|
||||
#[
|
||||
xxx move this to tests/stdlib/tasyncjs.nim
|
||||
]#
|
||||
|
||||
# demonstrate forward definition
|
||||
# for js
|
||||
proc y(e: int): Future[string] {.async.}
|
||||
import std/asyncjs
|
||||
|
||||
proc e: int {.discardable.} =
|
||||
echo "e"
|
||||
return 2
|
||||
block:
|
||||
# demonstrate forward definition for js
|
||||
proc y(e: int): Future[string] {.async.}
|
||||
|
||||
proc x(e: int): Future[void] {.async.} =
|
||||
var s = await y(e)
|
||||
if e > 2:
|
||||
return
|
||||
echo s
|
||||
e()
|
||||
proc e: int {.discardable.} =
|
||||
echo "e"
|
||||
return 2
|
||||
|
||||
proc y(e: int): Future[string] {.async.} =
|
||||
if e > 0:
|
||||
return await y(0)
|
||||
proc x(e: int): Future[void] {.async.} =
|
||||
var s = await y(e)
|
||||
if e > 2:
|
||||
return
|
||||
echo s
|
||||
e()
|
||||
|
||||
proc y(e: int): Future[string] {.async.} =
|
||||
if e > 0:
|
||||
return await y(0)
|
||||
else:
|
||||
return "x"
|
||||
|
||||
discard x(2)
|
||||
|
||||
import std/sugar
|
||||
from std/strutils import contains
|
||||
|
||||
var witness: seq[string]
|
||||
|
||||
proc fn(n: int): Future[int] {.async.} =
|
||||
if n >= 7:
|
||||
raise newException(ValueError, "foobar: " & $n)
|
||||
if n > 0:
|
||||
var ret = 1 + await fn(n-1)
|
||||
witness.add $(n, ret)
|
||||
return ret
|
||||
else:
|
||||
return "x"
|
||||
return 10
|
||||
|
||||
proc main() {.async.} =
|
||||
block: # then
|
||||
let x = await fn(4)
|
||||
.then((a: int) => a.float)
|
||||
.then((a: float) => $a)
|
||||
doAssert x == "14.0"
|
||||
doAssert witness == @["(1, 11)", "(2, 12)", "(3, 13)", "(4, 14)"]
|
||||
|
||||
discard x(2)
|
||||
doAssert (await fn(2)) == 12
|
||||
|
||||
let x2 = await fn(4).then((a: int) => (discard)).then(() => 13)
|
||||
doAssert x2 == 13
|
||||
|
||||
block: # catch
|
||||
var reason: Error
|
||||
await fn(6).then((a: int) => (witness.add $a)).catch((r: Error) => (reason = r))
|
||||
doAssert reason == nil
|
||||
|
||||
await fn(7).then((a: int) => (discard)).catch((r: Error) => (reason = r))
|
||||
doAssert reason != nil
|
||||
doAssert reason.name == "Error"
|
||||
doAssert "foobar: 7" in $reason.message
|
||||
echo "done" # justified here to make sure we're running this, since it's inside `async`
|
||||
|
||||
discard main()
|
||||
|
||||
22
tests/js/tasyncjs_fail.nim
Normal file
22
tests/js/tasyncjs_fail.nim
Normal file
@@ -0,0 +1,22 @@
|
||||
discard """
|
||||
exitCode: 1
|
||||
outputsub: "Error: unhandled exception: foobar: 13"
|
||||
"""
|
||||
|
||||
# note: this needs `--unhandled-rejections=strict`, see D20210217T215950
|
||||
|
||||
import std/asyncjs
|
||||
from std/sugar import `=>`
|
||||
|
||||
proc fn(n: int): Future[int] {.async.} =
|
||||
if n >= 7: raise newException(ValueError, "foobar: " & $n)
|
||||
else: result = n
|
||||
|
||||
proc main() {.async.} =
|
||||
let x1 = await fn(6)
|
||||
doAssert x1 == 6
|
||||
await fn(7).catch((a: Error) => (discard))
|
||||
let x3 = await fn(13)
|
||||
doAssert false # shouldn't go here, should fail before
|
||||
|
||||
discard main()
|
||||
Reference in New Issue
Block a user