Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async() should return invisibly? #46

Closed
shikokuchuo opened this issue Aug 8, 2024 · 1 comment · Fixed by #48
Closed

async() should return invisibly? #46

shikokuchuo opened this issue Aug 8, 2024 · 1 comment · Fixed by #48

Comments

@shikokuchuo
Copy link
Member

shikokuchuo commented Aug 8, 2024

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 Found

my_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

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!

@lionel-
Copy link
Member

lionel- commented Aug 21, 2024

I agree it probably makes sense to be consistent with promises here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants