pub async fn wait<T, F>(task: F) -> T
Expand description
Create a parallel task
that blocks awaiting for an IO operation, the task
starts on the first .await
.
§Parallel
The task
runs in the blocking
thread-pool which is optimized for awaiting blocking operations.
If the task
is computation heavy you should use run
and then wait
inside that task for the
parts that are blocking.
§Examples
task::wait(|| std::fs::read_to_string("file.txt")).await
The example reads a file, that is a blocking file IO operation, most of the time is spend waiting for the operating system,
so we offload this to a wait
task. The task can be .await
inside a run
task or inside one of the UI tasks
like in a async event handler.
§Async Read/Write
For std::io::Read
and std::io::Write
operations you can also use io
and fs
alternatives when you don’t
have or want the full file in memory or when you want to apply multiple operations to the file.
§Panic Propagation
If the task
panics the panic is resumed in the awaiting thread using resume_unwind
. You
can use wait_catch
to get the panic as an error instead.