# async-operation State-tracking wrapper for a single asynchronous process. [`AsyncOperation`](https://www.hyperfrontend.dev/docs/libraries/state-machine/async-operation/#api-AsyncOperation) is the runtime object that owns the state of one async process: not-started, in-progress, paused, done, failed, retrying, etc. It dispatches the actions in [`/actions`](https://www.hyperfrontend.dev/docs/libraries/state-machine/actions.md) against the reducer and exposes selectors so consumers can query the process state without importing the reducer directly. [`AsyncProcess`](https://www.hyperfrontend.dev/docs/libraries/state-machine/async-operation/#api-AsyncProcess) is the data shape held inside the operation, useful when the operation is composed into larger structures or persisted. Reach for the higher-level [`/coordinated-async-operation`](https://www.hyperfrontend.dev/docs/libraries/state-machine/coordinated-async-operation.md) when several related async processes need to be orchestrated together. ## API Reference ### Classes #### AsyncOperation Manages the lifecycle of an asynchronous operation with start, cancel, and pause capabilities. ##### Constructor ```typescript new AsyncOperation(process: AsyncProcess) ``` Wraps a process so its progress can be observed as state-machine events. Nothing runs until `start` is called. **Parameters** | Name | Type | Description | | --- | --- | --- | | `process` | `AsyncProcess` | The asynchronous work to manage; its returned promise decides success or failure | ##### cancel ```typescript readonly cancel(): void ``` Dispatches the cancel action, which leaves the state halted and no longer in progress. The promise returned by the process is not interrupted: the work keeps running, and whatever it settles with is still dispatched afterwards. Cancellation is a state the operation's observers see, not a signal the process receives. ##### on ```typescript readonly on(event: Event, handler: (event: Event, current: DerivedState, previous: DerivedState) => void): void ``` Registers a handler for one of the operation's lifecycle events. **Parameters** | Name | Type | Description | | --- | --- | --- | | `event` | `Event` | Which state transition to listen for, one of the `event` constants | | `handler` | `(event: Event, current: DerivedState, previous: DerivedState) => void` | Called with the event name, the state after the transition and the state before it | ##### pause ```typescript readonly pause(): void ``` Dispatches the pause action, which marks the state halted while still in progress. As with `cancel`, the process itself is not suspended; only the observable state changes. ##### start ```typescript readonly start(): Promise ``` Runs the process once, dispatching the start action before it and success or fail after. A rejection is caught and reported as a failure carrying the thrown value, so the returned promise never rejects. Calling `start` again runs the process again, and the events describe the rerun as a retry or a restart depending on how the previous run ended. **Returns** `Promise`: Resolves once the process has settled and its outcome has been dispatched **Example: Starting an async operation** ```typescript const operation = new AsyncOperation(async () => { await fetchData() }) operation.start() ``` ### Types #### AsyncProcess Async process function type that returns a Promise. ```typescript type AsyncProcess = () => Promise ``` --- Canonical page: https://www.hyperfrontend.dev/docs/libraries/state-machine/async-operation/ This file: https://www.hyperfrontend.dev/docs/libraries/state-machine/async-operation.md Documentation index: https://www.hyperfrontend.dev/llms.txt