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

Let async functions return invisibly #48

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# coro (development version)

* Async functions created by `coro::async()` now return their
`promises::promise()` invisibly (#46, @shikokuchuo).

# coro 1.0.4

* Internal fix for R-devel.
Expand Down
2 changes: 1 addition & 1 deletion R/async.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#'
#' @param fn An anonymous function within which `await()` calls are
#' allowed.
#' @return A function that returns a [promises::promise()].
#' @return A function that returns a [promises::promise()] invisibly.
#'
#' @seealso [async_generator()] and [await_each()];
#' [coro_debug()] for step-debugging.
Expand Down
2 changes: 1 addition & 1 deletion R/generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ generator0 <- function(fn, type = "generator") {

if (is_string(type, "async")) {
# Step into the generator right away
instance(NULL)
invisible(instance(NULL))
} else {
structure(instance, class = "coro_generator_instance")
}
Expand Down
2 changes: 1 addition & 1 deletion man/async.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-async.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,14 @@ test_that("can await-assign with `=` (#29)", {
})
expect_equal(wait_for(fn()), 2)
})

test_that("async function returns invisibly (#46)", {
out <- NULL
fn <- async(function() {
for (i in 1:3) {
out <<- c(out, i)
await(i)
}
})
expect_invisible(fn())
})
13 changes: 13 additions & 0 deletions tests/testthat/test-generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,16 @@ test_that("can yield-assign with `=` (#29)", {
i()
expect_equal(i("bar"), "bar")
})

test_that("stepping into generators returns visibly (#46)", {
generate_abc <- generator(function() {
yield("a")
yield("b")
"c"
})
abc <- generate_abc()
expect_visible(abc())
expect_visible(abc())
expect_visible(abc())
expect_visible(abc())
})
Loading