Skip to main content

spawn_async

Function spawn_async 

Source
pub fn spawn_async<TFn, TFuture, T>(f: TFn) -> JoinHandle<T>
where TFn: FnOnce() -> TFuture + Send + 'static, TFuture: Future<Output = T> + 'static, T: Send + 'static,
Expand description

Spawn a new thread that runs co-operatively with the JS event loop.

Unlike spawn, you can run asynchronous JS (e.g. with js_sys or web_sys) inside the thread and await it.

Note that this function does not directly take a future, but rather takes a closure that returns a future. This is because while the closure needs to be Send, the future does not. For more information, please refer to Send bounds in the book.

Conceptually, the new thread will start executing immediately without the need to yield to the JS Event Loop, meaning the spawning thread can block immediately after calling spawn_async to join the spawned thread without causing dead locks.

ยงPanics

Similar to std::thread::spawn, this function may panic if the thread creation fails, including if the thread dispatcher has not been initialized (see ThreadDispatcherInit). Use try_spawn_async as the recoverable version.