pub fn respond<R, F>(task: F) -> ResponseVar<R>Expand description
Spawn a parallel async task that will send its result to a ResponseVar<R>.
The run documentation explains how task is parallel and async. The task starts executing immediately.
This is just a helper method that creates a response_var and awaits for the task in a spawn runner.
§Examples
fn on_event(&mut self) {
self.sum_response = task::respond(async {
read_numbers().await.par_iter().map(|i| i * i).sum()
});
}
fn on_update(&mut self) {
if let Some(result) = self.sum_response.rsp_new() {
println!("sum of squares: {result}");
}
}The example .await for some numbers and then uses a parallel iterator to compute a result. The result is send to
sum_response that is a ResponseVar<R>.
§Cancellation
Dropping the ResponseVar<R> does not cancel the task, it will still run to completion.
§Panic Handling
If the task panics the panic is logged but otherwise ignored and the variable never responds. See
spawn for more information about the panic handling of this function.