Skip to main content

yield_now

Function yield_now 

Source
pub async fn yield_now()
Expand description

Yield the current thread, allowing other tasks to run.

The task is rescheduled as soon as the other ready tasks have run. Use this to slice long computations into quanta. A task that polls for an event should call yield_throttled instead, so that it does not keep a single-threaded host busy.

On wasm32-unknown-unknown this ends the current browser task: timers, input and rendering that are already due run before the task resumes, and it costs far less than a millisecond. Embassy’s own wasm yield is a microtask that would starve all of them, and a timer-backed yield costs 10 ms or more per call, which throttles anything that yields often (the Wasm runtime yields once per fuel quantum, so a timer made audio rendering choppy). Where the browser exposes no MessageChannel, it falls back to a zero-delay timer, and where there is no timer either (an AudioWorkletGlobalScope) to a microtask. The microtask fallback does not let the host run: a loop that only yields starves everything else in its scope, so such a scope must wait on something other than yield_now (and cannot use sleep, which needs a timer). On wasm32-wasip1 it sleeps 10 ms: there is no host event loop to give control to.