% Generated by roxygen2: do not edit by hand % Please edit documentation in R/then.R \name{then} \alias{then} \alias{catch} \alias{finally} \title{Access the results of a promise} \usage{ then(promise, onFulfilled = NULL, onRejected = NULL, ..., tee = FALSE) catch(promise, onRejected, ..., tee = FALSE) finally(promise, onFinally) } \arguments{ \item{promise}{A promise object. The object can be in any state.} \item{onFulfilled}{A function that will be invoked if the promise value successfully resolves. When invoked, the function will be called with a single argument: the resolved value. Optionally, the function can take a second parameter \code{.visible} if you care whether the promise was resolved with a visible or invisible value. The function can return a value or a promise object, or can throw an error; these will affect the resolution of the promise object that is returned by \code{then()}.} \item{onRejected}{A function taking the argument \code{error}. The function can return a value or a promise object, or can throw an error. If \code{onRejected} is provided and doesn't throw an error (or return a promise that fails) then this is the async equivalent of catching an error.} \item{...}{Ignored.} \item{tee}{If \code{TRUE}, ignore the return value of the callback, and use the original value instead. This is useful for performing operations with side-effects, particularly logging to the console or a file. If the callback itself throws an error, and \code{tee} is \code{TRUE}, that error will still be used to fulfill the the returned promise (in other words, \code{tee} only has an effect if the callback does not throw).} \item{onFinally}{A function with no arguments, to be called when the async operation either succeeds or fails. Usually used for freeing resources that were used during async operations.} } \description{ Use the \code{then} function to access the eventual result of a promise (or, if the operation fails, the reason for that failure). Regardless of the state of the promise, the call to \code{then} is non-blocking, that is, it returns immediately; so what it does \emph{not} do is immediately return the result value of the promise. Instead, you pass logic you want to execute to \code{then}, in the form of function callbacks. If you provide an \code{onFulfilled} callback, it will be called upon the promise's successful resolution, with a single argument \code{value}: the result value. If you provide an \code{onRejected} callback, it will be called if the operation fails, with a single argument \code{reason}: the error that caused the failure. } \section{Formulas}{ \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} 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 example, \href{https://github.com/tidyverse/purrr/commit/670c3ed9920f15da0d4175068ecddc41f0f1f335#diff-c4dcc43795da5c7f6bf5a94d957b5507ce795fedd6d3eb092ccad03678c4f76dR15}{\code{{purrr}}}) as we now have access to the function shorthand, \verb{\\(x) fn(x)}. Please update your code to use the new function shorthand syntax \verb{\\(x) fn(x, arg1, args2)} instead of \code{~ fn(., arg1, arg2)}. The \code{.} can be confusing when chained with other methods. } \section{Chaining promises}{ The first parameter of \code{then} is a promise; given the stated purpose of the function, this should be no surprise. However, what may be surprising is that the return value of \code{then} is also a (newly created) promise. This new promise waits for the original promise to be fulfilled or rejected, and for \code{onFulfilled} or \code{onRejected} to be called. The result of (or error raised by) calling \code{onFulfilled}/\code{onRejected} will be used to fulfill (reject) the new promise. \if{html}{\out{