# `Flezha.SDK.Spawn`
[🔗](https://github.com/flezha/flezha_elixir_sdk/blob/v0.1.0/lib/flezha/sdk/spawn.ex#L1)

Tasklet-requested spawn of a steps instance: the request a tasklet makes
with `Flezha.SDK.Ctx.spawn_steps/4`, the handle it gets back at once, and
what the controller's answer resolves to.

The request is `flz_core`'s, made from the invocation's live activation, and
the handle is the core's: it is read inside the invocation that made it, and
released when that invocation's activation completes. A failure nobody read
is logged once by the core. Nothing here faults the invocation: every
refusal resolves the handle. The contract is `docs/public/topics/core/workflows.md`.

# `if_running`

```elixir
@type if_running() :: :deliver | :error
```

What a running instance id gets: the payload, or `already_running`.

# `option`

```elixir
@type option() :: {:id, String.t()} | {:if_running, if_running()}
```

`id:` names the instance; without it the controller generates one and the
request always starts a new instance. `if_running:` needs an `id:` (the core
refuses it without one, `spawn.if_running_without_id`) and defaults to
`:deliver`.

# `result`

```elixir
@type result() ::
  {:ok, %{id: String.t(), outcome: :spawned | :delivered | :skipped_on_replay}}
  | {:error, Flezha.SDK.Error.t()}
```

What a handle resolves to: the instance reached, or the refusal, carrying
the controller's own code (`relay.already_running`, `relay.factory_unknown`,
...) or the core's (`spawn.timeout`, `spawn.unavailable`).

# `t`

```elixir
@type t() :: %Flezha.SDK.Spawn{
  core: term(),
  factory_id: term(),
  handle: term(),
  instance_id: term(),
  result: term()
}
```

A request's handle: the core's handle number, or the `result` known without
asking (a refusal at the call).

# `await`

```elixir
@spec await(t(), non_neg_integer()) :: result()
```

Wait for the answer, up to `timeout` milliseconds; `spawn.timeout` when
nothing arrived.

# `try_result`

```elixir
@spec try_result(t()) :: result() | :pending
```

The answer if it has arrived, else `:pending`, without waiting.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
