You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was creating a version of your readme example, but with an actual async download function i.e. nanonext::ncurl_aio() and using mirai to handle the external computation.
However, the promise object is returned visibly in the second coro example. This is as promises::then() returns invisibly. I propose that coro::async() can do the same?
I think this is fundamentally similar to #10. Happy to provide a PR if you'd like.
library(coro)
library(mirai)
my_async<- \(url) {
nanonext::ncurl_aio(url) |>promises::then(
\(data) {
if (nchar(data) >500L) {
promises::then(
mirai(rvest::html_text2(rvest::read_html(data)), data=data),
\(x) cat(x, "\n")
)
} else {
cat(data, "\n")
}
},
onRejected= \(err) cat(conditionMessage(err), "\n")
)
}
my_async("https://example.com/")
# Example Domain## This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.## More information...
my_async("https://postman-echo.com/get")
# {# "args": {},# "headers": {# "host": "postman-echo.com",# "x-request-start": "t=1723145472.652",# "x-forwarded-proto": "https",# "x-forwarded-port": "443",# "x-amzn-trace-id": "Root=1-66b51d00-3a00039803db97680dee8012"# },# "url": "https://postman-echo.com/get"# }
my_async("https://example.com/notfound")
# 404 | Not Foundmy_async2<- async(\(url) {
data<- tryCatch(await(nanonext::ncurl_aio(url)), error=identity)
if (inherits(data, "error"))
return(cat(conditionMessage(data), "\n"))
if (nchar(data) >500L)
data<- await(mirai(rvest::html_text2(rvest::read_html(data)), data=data))
cat(data, "\n")
})
my_async2("https://example.com/")
#> <Promise [pending]># Example Domain## This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.## More information...
my_async2("https://postman-echo.com/get")
#> <Promise [pending]># {# "args": {},# "headers": {# "host": "postman-echo.com",# "x-request-start": "t=1723145476.118",# "x-forwarded-proto": "https",# "x-forwarded-port": "443",# "x-amzn-trace-id": "Root=1-66b51d04-7a9ccbdd1efef37c687c38cc"# },# "url": "https://postman-echo.com/get"# }
my_async2("https://example.com/notfound")
#> <Promise [pending]># 404 | Not Found
I was creating a version of your readme example, but with an actual async download function i.e. nanonext::ncurl_aio() and using
mirai
to handle the external computation.However, the promise object is returned visibly in the second
coro
example. This is aspromises::then()
returns invisibly. I propose thatcoro::async()
can do the same?I think this is fundamentally similar to #10. Happy to provide a PR if you'd like.
Created on 2024-08-08 with reprex v2.1.1
Note: I've manually appended the stdout to the reprex above as the reprex package doesn't render the async output.
Btw. I think the brevity of the
coro
version of the code is really cool!The text was updated successfully, but these errors were encountered: