| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/utils.R |
| 3 |
\name{promise_map} |
| 4 |
\alias{promise_map} |
| 5 |
\title{Promise-aware lapply/map} |
| 6 |
\usage{ |
| 7 |
promise_map(.x, .f, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{.x}{A vector (atomic or list) or an expression object (but not a |
| 11 |
promise). Other objects (including classed objects) will be coerced by |
| 12 |
base::as.list.} |
| 13 |
|
| 14 |
\item{.f}{The function to be applied to each element of \code{.x}. The function is |
| 15 |
permitted, but not required, to return a promise.} |
| 16 |
|
| 17 |
\item{...}{Optional arguments to \code{.f}.} |
| 18 |
} |
| 19 |
\value{ |
| 20 |
A promise that resolves to a list (of values, not promises). |
| 21 |
} |
| 22 |
\description{ |
| 23 |
Similar to \code{\link[base:lapply]{base::lapply()}} or \code{\link[purrr:map]{purrr::map}}, but promise-aware: the \code{.f} |
| 24 |
function is permitted to return promises, and while \code{lapply} returns a list, |
| 25 |
\code{promise_map} returns a promise that resolves to a similar list (of resolved |
| 26 |
values only, no promises). |
| 27 |
} |
| 28 |
\details{ |
| 29 |
\code{promise_map} processes elements of \code{.x} serially; that is, if \code{.f(.x[[1]])} |
| 30 |
returns a promise, then \code{.f(.x[[2]])} will not be invoked until that promise |
| 31 |
is resolved. If any such promise rejects (errors), then the promise returned |
| 32 |
by \code{promise_map} immediately rejects with that err. |
| 33 |
} |
| 34 |
\examples{ |
| 35 |
# Waits x seconds, then returns x*10 |
| 36 |
wait_this_long <- function(x) { |
| 37 |
promise(\(resolve, reject) { |
| 38 |
later::later(\() resolve(x*10), delay = x) |
| 39 |
}) |
| 40 |
} |
| 41 |
|
| 42 |
promise_map( |
| 43 |
list(A=1, B=2, C=3), |
| 44 |
wait_this_long |
| 45 |
) |> |
| 46 |
then(print) |
| 47 |
|
| 48 |
} |