[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/flow](../../README) / [index](../README) / task

# Function: task()

> **task**(`_fn`, `context`): `void`

Defined in: [flow/src/decorators.ts:705](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/flow/src/decorators.ts#L705)

Marks an async method as a streaming, cancellable task. Implicitly `@expose` (callable from the
browser).

## Parameters

### \_fn

`unknown`

### context

`ClassMethodDecoratorContext`

## Returns

`void`

## Remarks

While a `@task` runs, the framework flushes throttled partial patches — so incremental field
writes such as `this.answer += token` appear live in the client — keeps the triggering control
in its loading state for the whole duration, and exposes `this.signal` (an `AbortSignal`) that
`this.cancel()` on the client trips for cooperative cancellation. Because it auto-exposes, no
separate `@expose` is needed. Applies to a (typically async) method.

## Example

```tsx
class Chat extends Component {
  @expose answer = "";

  @task async ask(prompt: string) {
    for await (const token of streamCompletion(prompt, this.signal)) {
      this.answer += token; // each write streams to the client
    }
  }
}
```
