% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/async.R
\name{rdesk_async}
\alias{rdesk_async}
\title{Run a task in the background}
\usage{
rdesk_async(
  task,
  args = list(),
  on_done = NULL,
  on_error = NULL,
  timeout_sec = NULL,
  app_id = NULL
)
}
\arguments{
\item{task}{A function to run in the background.}

\item{args}{A list of arguments to pass to the task.}

\item{on_done}{Callback function(result) called when the task finishes successfully.}

\item{on_error}{Callback function(error) called if the task fails.}

\item{timeout_sec}{Optional timeout in seconds. If exceeded, the job is
cancelled and \code{on_error()} receives a timeout error.}

\item{app_id}{Optional App ID used to associate a job with a specific app.}
}
\value{
Invisible job ID.
}
\description{
Automatically switches between 'mirai' (persistent daemons) and 'callr' (on-demand processes).
}
\examples{
# Fast, non-interactive task check (safe to unwrap)
rdesk_jobs_pending()

if (interactive()) {
  # Run a long-running computation in the background
  rdesk_async(
    task = function(n) { Sys.sleep(2); sum(runif(n)) },
    args = list(n = 1e6),
    on_done = function(res) message("Task finished: ", res),
    on_error = function(err) message("Task failed: ", err$message)
  )
}
}
