spawn_local

Function spawn_local 

Source
pub fn spawn_local<F: Future + 'static>(future: F, _debug_label: &'static str)
Expand description

Spawns a future in a platform-appropriate way without waiting for completion.

This function automatically selects the appropriate executor based on the target platform:

  • On native platforms (Linux, macOS, Windows, etc.): Uses sleep_on to run the future on the current thread
  • On wasm32 targets: Uses wasm_bindgen_futures::spawn_local to integrate with the browser’s event loop

§Parameters

  • future: The future to execute
  • _debug_label: A label for debugging purposes (used for logging context on WASM)

§Example

use test_executors::spawn_local;

spawn_local(async {
    // This will run correctly on both native and WASM platforms
    println!("Hello from async!");
}, "example_task");

§Platform Behavior

§Native Platforms

The future is executed immediately on the current thread using sleep_on. This blocks until the future completes.

§WebAssembly

The future is scheduled to run on the browser’s event loop and this function returns immediately. The future will run when the JavaScript runtime is ready.

§Note

Unlike spawn_on, this function does not require the future to be Send since it may run on the current thread.