Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

promise_map

Promise-aware lapply/map


Description

Similar to base::lapply() or purrr::map, but promise-aware: the .f function is permitted to return promises, and while lapply returns a list, promise_map returns a promise that resolves to a similar list (of resolved values only, no promises).

Usage

promise_map(.x, .f, ...)

Arguments

.x

A vector (atomic or list) or an expression object (but not a promise). Other objects (including classed objects) will be coerced by base::as.list.

.f

The function to be applied to each element of .x. The function is permitted, but not required, to return a promise.

...

Optional arguments to .f.

Details

promise_map processes elements of .x serially; that is, if .f(.x[[1]]) returns a promise, then .f(.x[[2]]) will not be invoked until that promise is resolved. If any such promise rejects (errors), then the promise returned by promise_map immediately rejects with that err.

Value

A promise that resolves to a list (of values, not promises).

Examples

# Waits x seconds, then returns x*10
wait_this_long <- function(x) {
  promise(~later::later(~{
    resolve(x*10)
  }, delay = x))
}

promise_map(list(A=1, B=2, C=3), wait_this_long) %...>%
  print()

promises

Abstractions for Promise-Based Asynchronous Programming

v1.2.0.1
MIT + file LICENSE
Authors
Joe Cheng [aut, cre], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.