Expand description
Glue between Swan promises and the host’s async runtime.
The engine runs imports as background tasks. It does not depend on any
runtime: the host passes a SpawnFn that starts a Task on its own
executor. Without one, tasks go onto a thread-local queue that the host
drains with run_pending.
promise_async is also useful to hosts: it turns a Rust future into a
Swan promise, so a host function can do asynchronous work.
§Examples
A host function that returns a promise:
use swan_core::value::{BuiltinFn, Value};
use texl_core::{Engine, helpers::promise_async};
let engine = Engine::new(None);
engine.add_builtin("fetch_answer", Value::Builtin(BuiltinFn::new("fetch_answer", |_| {
promise_async(None, async { Value::Numb(42.0) })
})));
assert_eq!(block_on(engine.render("<% fetch_answer() %>")), "42");Functions§
- promise_
async - Wraps an async operation in a background task that resolves a Swan
Promise. - run_
pending - Polls queued fallback tasks until no further progress is possible.
- spawn
- Runs
taskwithspawner, or queues it withspawn_defaultifspawnerisNone. - spawn_
default - Fallback spawner: queues the task on a thread-local queue.
- spawner_
fn - Builds a
SpawnFnfrom a plain closure.