# TaskScope

`import { TaskScope } from "destack:async";`

Structured owner of spawned tasks.

Asynchronous disposal waits for every child before it completes.
Cancellation requests are delivered cooperatively at cancellable asynchronous operations.

```ds title="TaskScope"
export struct TaskScope {
    static open(): TaskScope;
    spawn<T: Copy>(body: () => Task<T>): Task<T>;
    cancel(): void;
    async asyncDispose(): Promise<void>;
    drop(): void;
}
```

[language/library/src/async/task.ds:105:133](https://github.com/destack-sh/destack/blob/main/language/library/src/async/task.ds#L105-L133)

## Members

### `static open(): TaskScope`

Open a new task scope.

### `spawn<T: Copy>(body: () => Task<T>): Task<T>`

Spawn one task owned by this scope.

### `cancel(): void`

Request cancellation of every pending child.

### `async asyncDispose(): Promise<void>`

Cancel pending children and wait for their cleanup.

### `drop(): void`

Cancel pending children and detach their cleanup.
