Function async_wasm_task::spawn
source · pub fn spawn<F, T>(future: F) -> JoinHandle<T> ⓘwhere
F: Future<Output = T> + 'static,
T: 'static,Expand description
Spawns a new asynchronous task, returning a
JoinHandle for it.
The provided future will start running in the background immediately
when spawn is called, even if you don’t await the returned
JoinHandle.
Spawning a task enables the task to execute concurrently to other tasks. The
spawned task will always execute on the current thread,
as that’s how JavaScript’s Promise basically works.
Examples
In this example, a server is started and spawn is used to start a new task
that processes each received connection.
use std::io;
async fn process() -> io::Result<()> {
// Some process...
}
async fn start() -> io::Result<()> {
let result = async_wasm_task::spawn(async move {
// Process this job concurrently.
process(socket).await
}).await?;;
}To run multiple tasks in parallel and receive their results, join handles can be stored in a vector.
async fn my_background_op(id: i32) -> String {
let s = format!("Starting background task {}.", id);
println!("{}", s);
s
}
let ops = vec![1, 2, 3];
let mut tasks = Vec::with_capacity(ops.len());
for op in ops {
// This call will make them start running in the background
// immediately.
tasks.push(async_wasm_task::spawn(my_background_op(op)));
}
let mut outputs = Vec::with_capacity(tasks.len());
for task in tasks {
outputs.push(task.await.unwrap());
}
println!("{:?}", outputs);This example pushes the tasks to outputs in the order they were
started in.
Using !Send values from a task
The task supplied to spawn is not required to implement Send.
This is different from multi-threaded native async runtimes,
because JavaScript environment is inherently single-threaded.
For example, this will work:
use std::rc::Rc;
fn use_rc(rc: Rc<()>) {
// Do stuff w/ rc
}
async fn start() {
async_wasm_task::spawn(async {
// Force the `Rc` to stay in a scope with no `.await`
{
let rc = Rc::new(());
use_rc(rc.clone());
}
async_wasm_task::yield_now().await;
}).await.unwrap();
}This will work too, unlike multi-threaded native runtimes
where !Send values cannot live across .await:
use std::rc::Rc;
fn use_rc(rc: Rc<()>) {
// Do stuff w/ rc
}
]
async fn start() {
async_wasm_task::spawn(async {
let rc = Rc::new(());
async_wasm_task::yield_now().await;
use_rc(rc.clone());
}).await.unwrap();
}