Skip to content

Commit

Permalink
Let async functions return invisibly (#48)
Browse files Browse the repository at this point in the history
Closes #46
  • Loading branch information
shikokuchuo committed Aug 22, 2024
1 parent c29ba7d commit 8cf2232
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
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())
})

0 comments on commit 8cf2232

Please sign in to comment.