async: Make an async function

View source: R/async.R

asyncR Documentation

Make an async function

Description

async() functions are building blocks for cooperative concurrency.

  • They are concurrent because they are jointly managed by a scheduler in charge of running them.

  • They are cooperative because they decide on their own when they can no longer make quick progress and need to await some result. This is done with the await() keyword which suspends the async function and gives control back to the scheduler. The scheduler waits until the next async operation is ready to make progress.

The async framework used by async() functions is implemented in the later and promises packages:

  • You can chain async functions created with coro to promises.

  • You can await promises. You can also await futures created with the future package because they are coercible to promises.

Usage

async(fn)

await(x)

Arguments

fn

An anonymous function within which await() calls are allowed.

x

An awaitable value, i.e. a promise.

Value

A function that returns a promises::promise().

See Also

async_generator() and await_each(); coro_debug() for step-debugging.

Examples

# This async function counts down from `n`, sleeping for 2 seconds
# at each iteration:
async_count_down <- async(function(n) {
  while (n > 0) {
    cat("Down", n, "\n")
    await(async_sleep(2))
    n <- n - 1
  }
})

# This async function counts up until `stop`, sleeping for 0.5
# seconds at each iteration:
async_count_up <- async(function(stop) {
  n <- 1
  while (n <= stop) {
    cat("Up", n, "\n")
    await(async_sleep(0.5))
    n <- n + 1
  }
})

# You can run these functions concurrently using `promise_all()`
if (interactive()) {
  promises::promise_all(async_count_down(5), async_count_up(5))
}

coro documentation built on July 19, 2022, 5:06 p.m.