run_event_loop: Run event loop to completion

View source: R/synchronise.R

run_event_loopR Documentation

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.

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.

Value

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

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

r-lib/async documentation built on March 24, 2024, 6:20 p.m.