Function web_thread::web::spawn_async
source · pub fn spawn_async<F1, F2, T>(f: F1) -> JoinHandle<T>
Available on
Web
only.Expand description
Async version of spawn()
.
§Notes
Commonly a long-running thread is used by sending messages or tasks to it and blocking it when there is no work. Unfortunately this is often undesirable on the Web platform as it prevents yielding to the event loop.
Therefor being able to await
the next task instead of blocking the
thread is essential to build long-running threads on the Web platform.
§Panics
If the main thread does not support spawning threads, see
has_spawn_support()
.
§Example
let (sender, receiver) = async_channel::unbounded::<usize>();
web_thread::web::spawn_async(move || async move {
while let Ok(message) = receiver.recv().await {
web_sys::console::log_1(&message.into());
}
});
for message in 0..10 {
sender.try_send(message).unwrap();
}