| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/hybrid-then.R |
| 3 |
\name{hybrid_then} |
| 4 |
\alias{hybrid_then} |
| 5 |
\title{Asynchronous or synchronous \code{then()}} |
| 6 |
\usage{ |
| 7 |
hybrid_then(expr, on_success = NULL, on_failure = NULL, ..., tee = FALSE) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{expr}{An expression that evaluates to either a promise or a non-promise |
| 11 |
value.} |
| 12 |
|
| 13 |
\item{on_success}{A function to be called when no error occurs synchronously |
| 14 |
or asynchronously. When invoked, the function will be called with a single |
| 15 |
argument: the resolved value. Optionally, the function can take a second |
| 16 |
parameter \code{.visible} if you care whether the promise was resolved with a |
| 17 |
visible or invisible value. Can return a value or a promise.} |
| 18 |
|
| 19 |
\item{on_failure}{A function to be called if an error occurs synchronously or |
| 20 |
asynchronously. Takes one argument: the error object. Can return a value or |
| 21 |
a promise to recover from the error, or throw a new error. If \code{on_failure} |
| 22 |
is provided and doesn't throw an error (or return a promise that fails) |
| 23 |
then this is the async equivalent of catching an error.} |
| 24 |
|
| 25 |
\item{...}{Reserved for future use. Currently must be empty.} |
| 26 |
|
| 27 |
\item{tee}{If \code{TRUE}, ignore the return value of the callback, and use the |
| 28 |
original value of \code{expr} as the result. For \code{on_failure} with \code{tee = TRUE}, |
| 29 |
the callback executes but the original error is re-thrown afterward.} |
| 30 |
} |
| 31 |
\value{ |
| 32 |
\itemize{ |
| 33 |
\item If \code{expr} evaluates to a promise, a promise with a single followup |
| 34 |
promise to handle the \code{on_success} or \code{on_failure} callbacks. |
| 35 |
\item If \code{expr} evaluates to a non-promise value, the result of the synchronous |
| 36 |
operation after being processed by \code{on_success} or \code{on_failure}. |
| 37 |
\item If a callback returns a promise, the result is always a promise. |
| 38 |
} |
| 39 |
} |
| 40 |
\description{ |
| 41 |
This is a helper function that behaves like \code{then}, however if |
| 42 |
\code{is.promising()} returns \code{FALSE} then the handlers will be executed |
| 43 |
immediately. |
| 44 |
} |
| 45 |
\details{ |
| 46 |
Execution paths: |
| 47 |
\itemize{ |
| 48 |
\item If \code{expr} evaluates to a promise (\code{p}), it will call \code{p |> then(on_success, on_failure)}. |
| 49 |
\item If \code{expr} evaluates to a non-promise value (\code{x}), it will call |
| 50 |
\code{on_success(x)}. |
| 51 |
\item If \code{expr} throws an error (\code{e}) during calculation, it will call |
| 52 |
\code{on_failure(e)}. |
| 53 |
} |
| 54 |
|
| 55 |
In all cases, the \code{on_success} and \code{on_failure} callbacks are executed (when |
| 56 |
provided). |
| 57 |
} |
| 58 |
\section{Utility}{ |
| 59 |
|
| 60 |
|
| 61 |
This function is useful for writing functions that need to execute followup |
| 62 |
behavior \emph{now} or within a promise. This is different behavior than \code{then()} |
| 63 |
where \emph{everything} is made into a promise. |
| 64 |
|
| 65 |
\code{hybrid_then()} allows authors to keep synchronous execution on the same |
| 66 |
\emph{tick} without requiring the use of a followup promise. This is particularly |
| 67 |
appealing for situations where the author does not control the execution flow |
| 68 |
for items that may be either synchronous or asynchronous, such as within |
| 69 |
\code{{plumber2}}. |
| 70 |
} |
| 71 |
|
| 72 |
\section{Error Handling}{ |
| 73 |
|
| 74 |
|
| 75 |
If no \code{on_failure} callback is provided and an error occurs, the error is |
| 76 |
re-thrown immediately (for synchronous errors) or propagated through the |
| 77 |
returned promise (for asynchronous errors). |
| 78 |
|
| 79 |
If an \code{on_failure} callback is provided but it throws an error, that new |
| 80 |
error replaces the original error. With \code{tee = TRUE}, even if \code{on_failure} |
| 81 |
executes successfully, the original error is still re-thrown. |
| 82 |
} |
| 83 |
|
| 84 |
\section{Callback Return Values}{ |
| 85 |
|
| 86 |
|
| 87 |
Callbacks can return any value, including promises. If a callback returns a |
| 88 |
promise, the entire \code{hybrid_then()} call will return a promise, even if the |
| 89 |
input was synchronous. This allows seamless transitions between synchronous |
| 90 |
and asynchronous execution. |
| 91 |
} |
| 92 |
|
| 93 |
\examples{ |
| 94 |
# Basic usage - works with both sync and async values |
| 95 |
add_to <- function(x, k) { |
| 96 |
hybrid_then( |
| 97 |
x, |
| 98 |
on_success = function(value) { |
| 99 |
value + k |
| 100 |
}, |
| 101 |
on_failure = function(err) { |
| 102 |
message("Error: ", err$message) |
| 103 |
NA_real_ |
| 104 |
} |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
# Synchronous |
| 109 |
42 |> add_to(100) |
| 110 |
#> [1] 142 |
| 111 |
|
| 112 |
# Synchronous error |
| 113 |
add_to({stop("Bad input!")}, 8) |
| 114 |
#> Error: Bad input! |
| 115 |
#> [1] NA |
| 116 |
|
| 117 |
\dontrun{ |
| 118 |
# Asynchronous |
| 119 |
promise_resolve(42) |> |
| 120 |
add_to(8) |> |
| 121 |
then(print) |
| 122 |
# When resolved... |
| 123 |
#> [1] 50 |
| 124 |
|
| 125 |
# Error handling - asynchronous |
| 126 |
promise_resolve(stop("Bad async input!")) |> |
| 127 |
add_to(8) |> |
| 128 |
then(print) |
| 129 |
# When resolved... |
| 130 |
#> Error: Bad async input! |
| 131 |
#> [1] NA |
| 132 |
|
| 133 |
# Chaining multiple operations |
| 134 |
# (Move the `promise_resolve()` around to see sync vs async behavior) |
| 135 |
1 |> |
| 136 |
hybrid_then(on_success = \(x) x + 1) |> |
| 137 |
hybrid_then(on_success = \(x) promise_resolve(x * 2)) |> |
| 138 |
hybrid_then(on_success = \(x) x - 1) |> |
| 139 |
hybrid_then(print) |
| 140 |
# When resolved... |
| 141 |
#> [1] 3 |
| 142 |
} |
| 143 |
} |
| 144 |
\seealso{ |
| 145 |
\code{\link[=then]{then()}}, \code{\link[=is.promising]{is.promising()}}, \code{\link[=promise_resolve]{promise_resolve()}} |
| 146 |
} |