async_timeout: Asynchronous function call with a timeout

View source: R/timeout.R

async_timeoutR Documentation

Asynchronous function call with a timeout

Description

If the deferred value is not resolved before the timeout expires, async_timeout() throws an async_timeout error.

Usage

async_timeout(task, timeout, ...)

Arguments

task

Asynchronous function.

timeout

Timeout as a difftime object, or number of seconds.

...

Additional arguments to task.

Value

A deferred value. An async_timeout error is thrown if it is not resolved within the specified timeout.

Examples

## You can catch the error, asynchronously
synchronise(
  async_timeout(function() delay(1/10)$then(function() "OK"), 1/1000)$
    catch(async_timeout = function(e) "Timed out",
          error = function(e) "Other error")
)

## Or synchronously
tryCatch(
  synchronise(
    async_timeout(function() delay(1/10)$then(function() "OK"), 1/1000)
  ),
  async_timeout = function(e) "Timed out. :(",
  error = function(e) paste("Other error:", e$message)
)

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