Remove define for jsfetch (#19530)
* Remove define nimExperimentalAsyncjsThen for std/asyncjs.then and std/jsfetch * Remove define nimExperimentalAsyncjsThen for std/asyncjs.then and std/jsfetch * Remove define nimExperimentalAsyncjsThen for std/asyncjs.then and std/jsfetch * Remove define nimExperimentalAsyncjsThen for std/asyncjs.then and std/jsfetch
This commit is contained in:
parent
9c17a32e0e
commit
f0bfc0bd3f
4 changed files with 83 additions and 84 deletions
|
|
@ -40,7 +40,7 @@ becomes an alias for `addr`.
|
||||||
|
|
||||||
- Added `std/oserrors` for OS error reporting. Added `std/envvars` for environment variables handling.
|
- Added `std/oserrors` for OS error reporting. Added `std/envvars` for environment variables handling.
|
||||||
- Removed deprecated `oids.oidToString`.
|
- Removed deprecated `oids.oidToString`.
|
||||||
|
- Remove define `nimExperimentalAsyncjsThen` for `std/asyncjs.then` and `std/jsfetch`.
|
||||||
|
|
||||||
- Changed mimedb to use an `OrderedTable` instead of `OrderedTableRef`, to use it in a const.
|
- Changed mimedb to use an `OrderedTable` instead of `OrderedTableRef`, to use it in a const.
|
||||||
- Removed deprecated `jsre.test` and `jsre.toString`.
|
- Removed deprecated `jsre.test` and `jsre.toString`.
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ when not defined(js) and not defined(nimsuggest):
|
||||||
|
|
||||||
import std/jsffi
|
import std/jsffi
|
||||||
import std/macros
|
import std/macros
|
||||||
|
import std/private/since
|
||||||
|
|
||||||
type
|
type
|
||||||
Future*[T] = ref object
|
Future*[T] = ref object
|
||||||
|
|
@ -163,92 +164,91 @@ template maybeFuture(T): untyped =
|
||||||
when T is Future: T
|
when T is Future: T
|
||||||
else: Future[T]
|
else: Future[T]
|
||||||
|
|
||||||
when defined(nimExperimentalAsyncjsThen):
|
|
||||||
import std/private/since
|
|
||||||
since (1, 5, 1):
|
|
||||||
#[
|
|
||||||
TODO:
|
|
||||||
* map `Promise.all()`
|
|
||||||
* proc toString*(a: Error): cstring {.importjs: "#.toString()".}
|
|
||||||
|
|
||||||
Note:
|
since (1, 5, 1):
|
||||||
We probably can't have a `waitFor` in js in browser (single threaded), but maybe it would be possible
|
#[
|
||||||
in in nodejs, see https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
|
TODO:
|
||||||
and https://stackoverflow.com/questions/61377358/javascript-wait-for-async-call-to-finish-before-returning-from-function-witho
|
* map `Promise.all()`
|
||||||
]#
|
* proc toString*(a: Error): cstring {.importjs: "#.toString()".}
|
||||||
|
|
||||||
type Error* {.importjs: "Error".} = ref object of JsRoot
|
Note:
|
||||||
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
We probably can't have a `waitFor` in js in browser (single threaded), but maybe it would be possible
|
||||||
message*: cstring
|
in in nodejs, see https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
|
||||||
name*: cstring
|
and https://stackoverflow.com/questions/61377358/javascript-wait-for-async-call-to-finish-before-returning-from-function-witho
|
||||||
|
]#
|
||||||
|
|
||||||
type OnReject* = proc(reason: Error)
|
type Error* {.importjs: "Error".} = ref object of JsRoot
|
||||||
|
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||||
|
message*: cstring
|
||||||
|
name*: cstring
|
||||||
|
|
||||||
proc then*[T](future: Future[T], onSuccess: proc, onReject: OnReject = nil): auto =
|
type OnReject* = proc(reason: Error)
|
||||||
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
|
|
||||||
## Returns a `Future` from the return type of `onSuccess(T.default)`.
|
|
||||||
runnableExamples("-d:nimExperimentalAsyncjsThen"):
|
|
||||||
from std/sugar import `=>`
|
|
||||||
|
|
||||||
proc fn(n: int): Future[int] {.async.} =
|
proc then*[T](future: Future[T], onSuccess: proc, onReject: OnReject = nil): auto =
|
||||||
if n >= 7: raise newException(ValueError, "foobar: " & $n)
|
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
|
||||||
else: result = n * 2
|
## Returns a `Future` from the return type of `onSuccess(T.default)`.
|
||||||
|
runnableExamples("-r:off"):
|
||||||
|
from std/sugar import `=>`
|
||||||
|
|
||||||
proc asyncFact(n: int): Future[int] {.async.} =
|
proc fn(n: int): Future[int] {.async.} =
|
||||||
if n > 0: result = n * await asyncFact(n-1)
|
if n >= 7: raise newException(ValueError, "foobar: " & $n)
|
||||||
else: result = 1
|
else: result = n * 2
|
||||||
|
|
||||||
proc main() {.async.} =
|
proc asyncFact(n: int): Future[int] {.async.} =
|
||||||
block: # then
|
if n > 0: result = n * await asyncFact(n-1)
|
||||||
assert asyncFact(3).await == 3*2
|
else: result = 1
|
||||||
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
|
proc main() {.async.} =
|
||||||
var witness = 1
|
block: # then
|
||||||
await fn(6).then((a: int) => (witness = 2), (r: Error) => (witness = 3))
|
assert asyncFact(3).await == 3*2
|
||||||
assert witness == 2
|
assert asyncFact(3).then(asyncFact).await == 6*5*4*3*2
|
||||||
await fn(7).then((a: int) => (witness = 2), (r: Error) => (witness = 3))
|
let x1 = await fn(3)
|
||||||
assert witness == 3
|
assert x1 == 3 * 2
|
||||||
|
let x2 = await fn(4)
|
||||||
|
.then((a: int) => a.float)
|
||||||
|
.then((a: float) => $a)
|
||||||
|
assert x2 == "8.0"
|
||||||
|
|
||||||
template impl(call): untyped =
|
block: # then with `onReject` callback
|
||||||
# see D20210421T014713
|
var witness = 1
|
||||||
when typeof(block: call) is void:
|
await fn(6).then((a: int) => (witness = 2), (r: Error) => (witness = 3))
|
||||||
var ret: Future[void]
|
assert witness == 2
|
||||||
else:
|
await fn(7).then((a: int) => (witness = 2), (r: Error) => (witness = 3))
|
||||||
var ret = default(maybeFuture(typeof(call)))
|
assert witness == 3
|
||||||
typeof(ret)
|
|
||||||
when T is void:
|
template impl(call): untyped =
|
||||||
type A = impl(onSuccess())
|
# see D20210421T014713
|
||||||
|
when typeof(block: call) is void:
|
||||||
|
var ret: Future[void]
|
||||||
else:
|
else:
|
||||||
type A = impl(onSuccess(default(T)))
|
var ret = default(maybeFuture(typeof(call)))
|
||||||
var ret: A
|
typeof(ret)
|
||||||
asm "`ret` = `future`.then(`onSuccess`, `onReject`)"
|
when T is void:
|
||||||
return ret
|
type A = impl(onSuccess())
|
||||||
|
else:
|
||||||
|
type A = impl(onSuccess(default(T)))
|
||||||
|
var ret: A
|
||||||
|
asm "`ret` = `future`.then(`onSuccess`, `onReject`)"
|
||||||
|
return ret
|
||||||
|
|
||||||
proc catch*[T](future: Future[T], onReject: OnReject): Future[void] =
|
proc catch*[T](future: Future[T], onReject: OnReject): Future[void] =
|
||||||
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
|
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
|
||||||
runnableExamples("-d:nimExperimentalAsyncjsThen"):
|
runnableExamples("-r:off"):
|
||||||
from std/sugar import `=>`
|
from std/sugar import `=>`
|
||||||
from std/strutils import contains
|
from std/strutils import contains
|
||||||
|
|
||||||
proc fn(n: int): Future[int] {.async.} =
|
proc fn(n: int): Future[int] {.async.} =
|
||||||
if n >= 7: raise newException(ValueError, "foobar: " & $n)
|
if n >= 7: raise newException(ValueError, "foobar: " & $n)
|
||||||
else: result = n * 2
|
else: result = n * 2
|
||||||
|
|
||||||
proc main() {.async.} =
|
proc main() {.async.} =
|
||||||
var reason: Error
|
var reason: Error
|
||||||
await fn(6).catch((r: Error) => (reason = r)) # note: `()` are needed, `=> reason = r` would not work
|
await fn(6).catch((r: Error) => (reason = r)) # note: `()` are needed, `=> reason = r` would not work
|
||||||
assert reason == nil
|
assert reason == nil
|
||||||
await fn(7).catch((r: Error) => (reason = r))
|
await fn(7).catch((r: Error) => (reason = r))
|
||||||
assert reason != nil
|
assert reason != nil
|
||||||
assert "foobar: 7" in $reason.message
|
assert "foobar: 7" in $reason.message
|
||||||
|
|
||||||
discard main()
|
discard main()
|
||||||
|
|
||||||
asm "`result` = `future`.catch(`onReject`)"
|
asm "`result` = `future`.catch(`onReject`)"
|
||||||
|
|
|
||||||
|
|
@ -190,12 +190,11 @@ runnableExamples("-r:off"):
|
||||||
|
|
||||||
discard example()
|
discard example()
|
||||||
|
|
||||||
when defined(nimExperimentalAsyncjsThen):
|
block:
|
||||||
block:
|
proc example2 {.async.} =
|
||||||
proc example2 {.async.} =
|
await fetch("https://api.github.com/users/torvalds".cstring)
|
||||||
await fetch("https://api.github.com/users/torvalds".cstring)
|
.then((response: Response) => response.json())
|
||||||
.then((response: Response) => response.json())
|
.then((json: JsObject) => console.log(json))
|
||||||
.then((json: JsObject) => console.log(json))
|
.catch((err: Error) => console.log("Request Failed", err))
|
||||||
.catch((err: Error) => console.log("Request Failed", err))
|
|
||||||
|
|
||||||
discard example2()
|
discard example2()
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ const
|
||||||
gaCode* = " --doc.googleAnalytics:UA-48159761-1"
|
gaCode* = " --doc.googleAnalytics:UA-48159761-1"
|
||||||
# errormax: subsequent errors are probably consequences of 1st one; a simple
|
# errormax: subsequent errors are probably consequences of 1st one; a simple
|
||||||
# bug could cause unlimited number of errors otherwise, hard to debug in CI.
|
# bug could cause unlimited number of errors otherwise, hard to debug in CI.
|
||||||
docDefines = "-d:nimExperimentalAsyncjsThen -d:nimExperimentalLinenoiseExtra"
|
docDefines = "-d:nimExperimentalLinenoiseExtra"
|
||||||
nimArgs = "--errormax:3 --hint:Conf:off --hint:Path:off --hint:Processing:off --hint:XDeclaredButNotUsed:off --warning:UnusedImport:off -d:boot --putenv:nimversion=$# $#" % [system.NimVersion, docDefines]
|
nimArgs = "--errormax:3 --hint:Conf:off --hint:Path:off --hint:Processing:off --hint:XDeclaredButNotUsed:off --warning:UnusedImport:off -d:boot --putenv:nimversion=$# $#" % [system.NimVersion, docDefines]
|
||||||
gitUrl = "https://github.com/nim-lang/Nim"
|
gitUrl = "https://github.com/nim-lang/Nim"
|
||||||
docHtmlOutput = "doc/html"
|
docHtmlOutput = "doc/html"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue