Creates a new event loop, evaluates expr in it, and then runs the event loop to completion. It stops when the event loop does not have any tasks.

run_event_loop(expr)

Arguments

expr

Expression to run after creating a new event loop.

Value

NULL, always. If the event loop is to return some value, you can use lexical scoping, see the example below.

Details

The expression typically creates event loop tasks. It should not create deferred values, though, because those will never be evaluated.

Unhandled errors propagate to the run_event_loop() call, which fails.

In case of an (unhandled) error, all event loop tasks will be cancelled.

Examples

counter <- 0L
do <- function() {
  callback <- function() {
    counter <<- counter + 1L
    if (runif(1) < 1/10) t$cancel()
  }
  t <- async_timer$new(1/1000, callback)
}
run_event_loop(do())
counter
#> [1] 8