Skip to main content

spawn

Function spawn 

Source
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,
Expand description

Spawn a new thread similar to std::thread::spawn

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 to join the spawned thread without causing dead locks.

The closure f will be executed synchronously in the worker’s context. When f finishes (or panics), the worker is terminated. This means any promise/futures scheduled onto the JS Event Loop will not run and attempting to await them inside the thread will cause a dead lock. If you need to spawn a worker thread and run asynchronous JS (e.g. via js_sys or web_sys), use spawn_async to run the Rust thread co-operatively with the JS event loop.

§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 as the recoverable version.