async (version 0.0.0.9004)

run_event_loop: Run event loop to completion

Description

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.

Usage

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

Run this code
# NOT RUN {
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
# }

Run the code above in your browser using DataCamp Workspace