Interaction of browser, js, and https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/fn.spawn_local.html

Can someone please explain to me how the interaction of the browser, the js runtime, the rust wasmtime, and spawn_local in wasm_bindgen_futures - Rust work together ?

My current intuition (based on guessing) is:

  1. we store this future on some queue
  2. there is some rust/JS 'ticker', similar to a .requestAnimaionFrame, that every now and then goes through the queue and runs the asyncs

Based on a cursory look it seems like it polls the future once and then when you turn a javascript Promise into a JsFuture it will generate a refcounted state. This state will optionally contain a task waker. When creating the JsFuture .then() is called on the Promise with a closure that signals the task waker on completion and stores the result in the refcounted state. When you poll a JsFuture it will either return the result value if the promise was already completed or store the current task waker in the state of the JsFuture. spawn_local will The task waker will queue the task onto a global queue. This queue works by calling .then() on a Promise already resolved to the undefined JS value. I would guess this is done to prevent reentrancy problems in case a future immediately calls the waker and to keep the stack smaller.