Deploy to GitHub pages
This commit is contained in:
commit
2ef2c469d1
850 changed files with 332608 additions and 0 deletions
35
nimcache/runnableExamples/asyncjs_examples_1.nim
Normal file
35
nimcache/runnableExamples/asyncjs_examples_1.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#[
|
||||
autogenerated by docgen
|
||||
loc: /home/runner/work/Nim/Nim/lib/js/asyncjs.nim(203, 21)
|
||||
rdoccmd: -r:off
|
||||
]#
|
||||
import "/home/runner/work/Nim/Nim/lib/js/asyncjs.nim"
|
||||
{.line: ("/home/runner/work/Nim/Nim/lib/js/asyncjs.nim", 203, 21).}:
|
||||
from std/sugar import `=>`
|
||||
|
||||
proc fn(n: int): Future[int] {.async.} =
|
||||
if n >= 7: raise newException(ValueError, "foobar: " & $n)
|
||||
else: result = n * 2
|
||||
|
||||
proc asyncFact(n: int): Future[int] {.async.} =
|
||||
if n > 0: result = n * await asyncFact(n-1)
|
||||
else: result = 1
|
||||
|
||||
proc main() {.async.} =
|
||||
block: # then
|
||||
assert asyncFact(3).await == 3*2
|
||||
assert asyncFact(3).then(asyncFact).await == 6*5*4*3*2
|
||||
let x1 = await fn(3)
|
||||
assert x1 == 3 * 2
|
||||
let x2 = await fn(4)
|
||||
.then((a: int) => a.float)
|
||||
.then((a: float) => $a)
|
||||
assert x2 == "8.0"
|
||||
|
||||
block: # then with `onReject` callback
|
||||
var witness = 1
|
||||
await fn(6).then((a: int) => (witness = 2), (r: Error) => (witness = 3))
|
||||
assert witness == 2
|
||||
await fn(7).then((a: int) => (witness = 2), (r: Error) => (witness = 3))
|
||||
assert witness == 3
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue