Ship

Book · Tasks

Tasks

Draft

Start child work with spawn and join it with await.

On this page · 2 sections

Spawn and await

spawn { ... } starts a child fiber and returns Async.Task(T). await joins that task and produces its successful value. Neither form colors the surrounding function.

let twicetwice: fn(value: Int) -> Int = fn(valuevalue: Int) {
  let tasktask: Async.Task(Int) = spawn {
    valuevalue: Int * 22: Int
  };
  await tasktask: Async.Task(Int)
};

Spawn And Await

test✓
  let tasktask: Async.Task(Int) = spawn {
    4242: Int
  };
  ✓tasktask: Async.Task(Int): Async.Task(Int);
  expect ✓await tasktask: Async.Task(Int) == 4242: Int;
  expect ✓twicetwice: fn(value: Int) -> Int(2121: Int) == 4242: Int;

Nested tasks

A spawned block is its own return boundary. Its children belong to that child in the same way it belongs to its parent.

Nested Tasks

test✓
  let outerouter: Async.Task(Int) = spawn {
    let innerinner: Async.Task(Int) = spawn { return 4141: Int; };
    return await innerinner: Async.Task(Int) + 11: Int;
  };
  expect ✓await outerouter: Async.Task(Int) == 4242: Int;