Skip to content

taskState()

Creates a reactive task execution tracker with automatic status, duration, and error tracking.

Signature

ts
function taskState<T = unknown>(opts?: { id?: string }): TaskState<T>

Parameters

ParameterTypeDescription
opts{ id?: string }Optional configuration.

Returns

TaskState&lt;T&gt; — a task tracker with the following API:

MethodSignatureDescription
run(fn)(fn: (signal: AbortSignal) =&gt; T \Promise<T>) => Promise<T>
get()() =&gt; TaskMeta&lt;T&gt;Returns the current metadata snapshot (status, result, error, duration, runCount).
statusStore&lt;TaskStatus&gt;Reactive store: 'idle' \
resultStore&lt;T \undefined>
errorStore&lt;unknown \undefined>
durationStore&lt;number \undefined>
runCountStore&lt;number&gt;Reactive store of total run count.
reset()() =&gt; voidReset to idle state and abort any running task.
destroy()() =&gt; voidTear down all reactive stores.

Basic Usage

ts
import { taskState } from 'callbag-recharge';

const task = taskState<string>();
await task.run((signal) => fetch('/api', { signal }).then(r => r.text()));
task.status.get(); // 'success'
task.duration.get(); // e.g. 120

Options / Behavior Details

  • Signal-first: The run() callback receives an AbortSignal as its first argument for cooperative cancellation.
  • Companion stores: Each metadata field is an individual reactive store, independently subscribable.
  • Generation tracking: Concurrent reset() during a run() silently discards the stale result.

See Also

Released under the MIT License.