| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/then.R |
| 3 |
\name{then} |
| 4 |
\alias{then} |
| 5 |
\alias{catch} |
| 6 |
\alias{finally} |
| 7 |
\title{Access the results of a promise} |
| 8 |
\usage{ |
| 9 |
then(promise, onFulfilled = NULL, onRejected = NULL, ..., tee = FALSE) |
| 10 |
|
| 11 |
catch(promise, onRejected, ..., tee = FALSE) |
| 12 |
|
| 13 |
finally(promise, onFinally) |
| 14 |
} |
| 15 |
\arguments{ |
| 16 |
\item{promise}{A promise object. The object can be in any state.} |
| 17 |
|
| 18 |
\item{onFulfilled}{A function that will be invoked if the promise value |
| 19 |
successfully resolves. When invoked, the function will be called with a |
| 20 |
single argument: the resolved value. Optionally, the function can take a |
| 21 |
second parameter \code{.visible} if you care whether the promise was resolved |
| 22 |
with a visible or invisible value. The function can return a value or a |
| 23 |
promise object, or can throw an error; these will affect the resolution of |
| 24 |
the promise object that is returned by \code{then()}.} |
| 25 |
|
| 26 |
\item{onRejected}{A function taking the argument \code{error}. The function can |
| 27 |
return a value or a promise object, or can throw an error. If \code{onRejected} |
| 28 |
is provided and doesn't throw an error (or return a promise that fails) |
| 29 |
then this is the async equivalent of catching an error.} |
| 30 |
|
| 31 |
\item{...}{Ignored.} |
| 32 |
|
| 33 |
\item{tee}{If \code{TRUE}, ignore the return value of the callback, and use the |
| 34 |
original value instead. This is useful for performing operations with |
| 35 |
side-effects, particularly logging to the console or a file. If the |
| 36 |
callback itself throws an error, and \code{tee} is \code{TRUE}, that error will still |
| 37 |
be used to fulfill the the returned promise (in other words, \code{tee} only has |
| 38 |
an effect if the callback does not throw).} |
| 39 |
|
| 40 |
\item{onFinally}{A function with no arguments, to be called when the async |
| 41 |
operation either succeeds or fails. Usually used for freeing resources that |
| 42 |
were used during async operations.} |
| 43 |
} |
| 44 |
\description{ |
| 45 |
Use the \code{then} function to access the eventual result of a promise (or, if |
| 46 |
the operation fails, the reason for that failure). Regardless of the state of |
| 47 |
the promise, the call to \code{then} is non-blocking, that is, it returns |
| 48 |
immediately; so what it does \emph{not} do is immediately return the result value |
| 49 |
of the promise. Instead, you pass logic you want to execute to \code{then}, in the |
| 50 |
form of function callbacks. If you provide an |
| 51 |
\code{onFulfilled} callback, it will be called upon the promise's successful |
| 52 |
resolution, with a single argument \code{value}: the result value. If you provide |
| 53 |
an \code{onRejected} callback, it will be called if the operation fails, with a |
| 54 |
single argument \code{reason}: the error that caused the failure. |
| 55 |
} |
| 56 |
\section{Formulas}{ |
| 57 |
|
| 58 |
|
| 59 |
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} |
| 60 |
|
| 61 |
With \code{{promises}} depending on R >= 4.1, the shorthand of a formula, \code{~ fn(.)} is no longer recommended by the \code{{promises}} package or tidyverse (for |
| 62 |
example, |
| 63 |
\href{https://github.com/tidyverse/purrr/commit/670c3ed9920f15da0d4175068ecddc41f0f1f335#diff-c4dcc43795da5c7f6bf5a94d957b5507ce795fedd6d3eb092ccad03678c4f76dR15}{\code{{purrr}}}) |
| 64 |
as we now have access to the function shorthand, \verb{\\(x) fn(x)}. Please update |
| 65 |
your code to use the new function shorthand syntax \verb{\\(x) fn(x, arg1, args2)} |
| 66 |
instead of \code{~ fn(., arg1, arg2)}. The \code{.} can be confusing when chained with |
| 67 |
other methods. |
| 68 |
} |
| 69 |
|
| 70 |
\section{Chaining promises}{ |
| 71 |
|
| 72 |
|
| 73 |
The first parameter of \code{then} is a promise; given the stated purpose of the |
| 74 |
function, this should be no surprise. However, what may be surprising is that |
| 75 |
the return value of \code{then} is also a (newly created) promise. This new |
| 76 |
promise waits for the original promise to be fulfilled or rejected, and for |
| 77 |
\code{onFulfilled} or \code{onRejected} to be called. The result of (or error raised |
| 78 |
by) calling \code{onFulfilled}/\code{onRejected} will be used to fulfill (reject) the |
| 79 |
new promise. |
| 80 |
|
| 81 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{promise_a <- get_data_frame_async() |
| 82 |
promise_b <- then(promise_a, onFulfilled = head) |
| 83 |
}\if{html}{\out{</div>}} |
| 84 |
|
| 85 |
In this example, assuming \code{get_data_frame_async} returns a promise that |
| 86 |
eventually resolves to a data frame, \code{promise_b} will eventually resolve to |
| 87 |
the first 10 or fewer rows of that data frame. |
| 88 |
|
| 89 |
Note that the new promise is considered fulfilled or rejected based on |
| 90 |
whether \code{onFulfilled}/\code{onRejected} returns a value or throws an error, not on |
| 91 |
whether the original promise was fulfilled or rejected. In other words, it's |
| 92 |
possible to turn failure to success and success to failure. Consider this |
| 93 |
example, where we expect \code{some_async_operation} to fail, and want to consider |
| 94 |
it an error if it doesn't: |
| 95 |
|
| 96 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{promise_c <- some_async_operation() |
| 97 |
promise_d <- then(promise_c, |
| 98 |
onFulfilled = function(value) \{ |
| 99 |
stop("That's strange, the operation didn't fail!") |
| 100 |
\}, |
| 101 |
onRejected = function(reason) \{ |
| 102 |
# Great, the operation failed as expected |
| 103 |
NULL |
| 104 |
\} |
| 105 |
) |
| 106 |
}\if{html}{\out{</div>}} |
| 107 |
|
| 108 |
Now, \code{promise_d} will be rejected if \code{promise_c} is fulfilled, and vice |
| 109 |
versa. |
| 110 |
|
| 111 |
\strong{Warning:} Be very careful not to accidentally turn failure into success, |
| 112 |
if your error handling code is not the last item in a chain! |
| 113 |
|
| 114 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{some_async_operation() |> |
| 115 |
catch(function(reason) \{ |
| 116 |
warning("An error occurred: ", reason) |
| 117 |
\}) |> |
| 118 |
then(function() \{ |
| 119 |
message("I guess we succeeded...?") # No! |
| 120 |
\}) |
| 121 |
}\if{html}{\out{</div>}} |
| 122 |
|
| 123 |
In this example, the \code{catch} callback does not itself throw an error, so the |
| 124 |
subsequent \code{then} call will consider its promise fulfilled! |
| 125 |
} |
| 126 |
|
| 127 |
\section{Convenience functions}{ |
| 128 |
|
| 129 |
|
| 130 |
For readability and convenience, we provide \code{catch} and \code{finally} functions. |
| 131 |
|
| 132 |
The \code{catch} function is equivalent to \code{then}, but without the \code{onFulfilled} |
| 133 |
argument. It is typically used at the end of a promise chain to perform error |
| 134 |
handling/logging. |
| 135 |
|
| 136 |
The \code{finally} function is similar to \code{then}, but takes a single no-argument |
| 137 |
function that will be executed upon completion of the promise, |
| 138 |
regardless of whether the result is success or failure. It is typically used |
| 139 |
at the end of a promise chain to perform cleanup tasks, like closing file |
| 140 |
handles or database connections. Unlike \code{then} and \code{catch}, the return value |
| 141 |
of \code{finally} is ignored; however, if an error is thrown in \code{finally}, that |
| 142 |
error will be propagated forward into the returned promise. |
| 143 |
} |
| 144 |
|
| 145 |
\section{Visibility}{ |
| 146 |
|
| 147 |
|
| 148 |
\code{onFulfilled} functions can optionally have a second parameter \code{visible}, |
| 149 |
which will be \code{FALSE} if the result value is \link[base:invisible]{invisible}. |
| 150 |
} |
| 151 |
|