Method in task · Scope compiler

Scope::spawn

pub fn spawn<R>(unit -> R body) -> Task<R>
Import
use task::{Scope};
Source: compiler/src/prelude/task.hy

Starts body as a child task of this scope and returns its handle.

The task begins running at the caller’s next suspension point (an IO wait, sleep, join, yield_now or the end of the scope). Capture what it needs with use (…).

Called on a Scope value as value.spawn(…).

Parameters#

NameType
bodyunit -> R

Returns#

Task<R>

Examples#

use task::{scope, Scope};
use tests::assert;
let total = scope(fn (Scope s) {
    let a = s.spawn(fn () => 20);
    let b = s.spawn(fn () => 22);
    return (a.join() ?? 0) + (b.join() ?? 0);
});
assert!((total ?? 0) == 42);

See also#

↑ ↓ navigate ↵ open