Skip to content

useWorker

useWorker<T>(): UseWorkerResult<T>

Defined in: react/use-worker.ts:50

A cancellable async-task primitive for the UI — the agent-loop analogue of Textual’s @work. Exactly one run is in flight at a time: calling UseWorkerResult.run again aborts the previous run, and a superseded run can no longer change the state (“latest wins”). The task is handed an AbortSignal, so well-behaved work (fetches, model calls) stops promptly; the run is also aborted automatically when the component unmounts.

const job = useWorker<string>();
// …
job.run((signal) => callModel(prompt, { signal }));
// render job.status / job.isRunning / job.data, and a Cancel button → job.cancel()

T = unknown

UseWorkerResult<T>