| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/promise.R |
| 3 |
\name{promise} |
| 4 |
\alias{promise} |
| 5 |
\title{Create a new promise object} |
| 6 |
\usage{ |
| 7 |
promise(action) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{action}{A function with signature \verb{function(resolve, reject)}.} |
| 11 |
} |
| 12 |
\value{ |
| 13 |
A promise object (see \code{\link{then}}). |
| 14 |
} |
| 15 |
\description{ |
| 16 |
\code{promise()} creates a new promise. A promise is a placeholder object for the |
| 17 |
eventual result (or error) of an asynchronous operation. This function is not |
| 18 |
generally needed to carry out asynchronous programming tasks; instead, it is |
| 19 |
intended to be used mostly by package authors who want to write asynchronous |
| 20 |
functions that return promises. |
| 21 |
} |
| 22 |
\details{ |
| 23 |
The \code{action} function should be a piece of code that returns quickly, but |
| 24 |
initiates a potentially long-running, asynchronous task. If/when the task |
| 25 |
successfully completes, call \code{resolve(value)} where \code{value} is the result of |
| 26 |
the computation (like the return value). If the task fails, call |
| 27 |
\code{reject(reason)}, where \code{reason} is either an error object, or a character |
| 28 |
string. |
| 29 |
|
| 30 |
It's important that asynchronous tasks kicked off from \code{action} be coded very |
| 31 |
carefully--in particular, all errors must be caught and passed to \code{reject()}. |
| 32 |
Failure to do so will cause those errors to be lost, at best; and the caller |
| 33 |
of the asynchronous task will never receive a response (the asynchronous |
| 34 |
equivalent of a function call that never returns, i.e. hangs). |
| 35 |
|
| 36 |
The return value of \code{action} will be ignored. |
| 37 |
} |
| 38 |
\section{\verb{action=} formulas}{ |
| 39 |
|
| 40 |
|
| 41 |
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} |
| 42 |
|
| 43 |
With \code{{promises}} depending on R >= 4.1, the shorthand of a formula, \code{~ fn(.)} for \code{action} is no longer recommended by the \code{{promises}} package or |
| 44 |
tidyverse (for example, |
| 45 |
\href{https://github.com/tidyverse/purrr/commit/670c3ed9920f15da0d4175068ecddc41f0f1f335#diff-c4dcc43795da5c7f6bf5a94d957b5507ce795fedd6d3eb092ccad03678c4f76dR15}{\code{{purrr}}}) |
| 46 |
as we now have access to the function shorthand, \verb{\\(x) fn(x)}. Please update |
| 47 |
your \code{action} code to use the new function shorthand syntax \verb{\\(resolve, reject) resolve(arg1, arg2)} instead of \code{~ { resolve(arg1, arg2) }}. The |
| 48 |
magically created \code{resolve}/\code{reject} functions can be confusing when chained |
| 49 |
with other methods. |
| 50 |
} |
| 51 |
|
| 52 |
\examples{ |
| 53 |
# Create a promise that resolves to a random value after 2 secs |
| 54 |
p1 <- promise(\(resolve, reject) { |
| 55 |
later::later(\() resolve(runif(1)), delay = 2) |
| 56 |
}) |
| 57 |
|
| 58 |
p1 |> then(print) |
| 59 |
|
| 60 |
# Create a promise that errors immediately |
| 61 |
p2 <- promise(\(resolve, reject) { |
| 62 |
reject("An error has occurred") |
| 63 |
}) |
| 64 |
then(p2, |
| 65 |
onFulfilled = \(value) message("Success"), |
| 66 |
onRejected = \(err) message("Failure") |
| 67 |
) |
| 68 |
|
| 69 |
} |