followup #16871 asyncjs.then: allow pipelining procs returning futures (#17189)

* followup #16871 asyncjs.then: allow pipelining procs returning futures
* rename test files where they belong
* fix tests
* tests for then with `onReject` callback
* rename test file containing fail to avoid messing with grep
* address comments
* cleanup
* un-disable 1 test
This commit is contained in:
Timothee Cour 2021-03-04 05:00:04 -08:00 • committed by GitHub
commit a66637bda4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 28 deletions

View file

@ -50,6 +50,14 @@ proc fn(n: int): Future[int] {.async.} =
else:
return 10
proc asyncFact(n: int): Future[int] {.async.} =
if n > 0: result = n * await asyncFact(n-1)
else: result = 1
proc asyncIdentity(n: int): Future[int] {.async.} =
if n > 0: result = 1 + await asyncIdentity(n-1)
else: result = 0
proc main() {.async.} =
block: # then
let x = await fn(4)
@ -63,6 +71,18 @@ proc main() {.async.} =
let x2 = await fn(4).then((a: int) => (discard)).then(() => 13)
doAssert x2 == 13
let x4 = await asyncFact(3).then(asyncIdentity).then(asyncIdentity).then((a:int) => a * 7).then(asyncIdentity)
doAssert x4 == 3 * 2 * 7
block: # bug #17177
proc asyncIdentityNested(n: int): Future[int] {.async.} = return n
let x5 = await asyncFact(3).then(asyncIdentityNested)
doAssert x5 == 3 * 2
when false: # xxx pending bug #17254
let x6 = await asyncFact(3).then((a:int) {.async.} => a * 11)
doAssert x6 == 3 * 2 * 11
block: # catch
var reason: Error
await fn(6).then((a: int) => (witness.add $a)).catch((r: Error) => (reason = r))

View file

@ -5,6 +5,8 @@ t
'''
"""
# xxx merge into tasyncjs.nim
import asyncjs, macros
macro f*(a: untyped): untyped =