# Task

```ts
interface Task<T> extends Future<T>
```

A handle to a concurrently running operation that lets you either use the
result of that operation, or shut it down.

When it is run or spawned, an operation executes concurrently with
the rest of the program. The `Task` is both an [Operation](https://frontside.com/effection/api/v3/Operation.md) and a
`Promise` that lets you consume the result of that operation.

### Examples
#### Example 1
```javascript
import { run, sleep } from "effection";

let task = run(function*() {
  yield* sleep(100);
  return "hello world"
});

console.log(await task); //=> "hello world"
```

A task can also be created from within an operation by using the
[spawn(](https://frontside.com/effection/api/v3/spawn.md) operation.

---
#### Example 2
```javascript
import { run, spawn, sleep } from "effection";

await run(function*() {
  let task = yield* spawn(function*() {
    yield* sleep(100);
    return "hello world";
  });
  console.log(yield* task;) //=> "hello world"
});
```

Note tasks are subject to the strict guarantees of structured concurrency
and will never outlive their parent. For example, the following spawned task
will never log any output to the console.

---
#### Example 3
```javascript
import { run, spawn, sleep } from "effection";

await run(function*() {
  yield* spawn(function*() {
    yield* sleep(100);
    console.log("hello world");
  });
  // <--- returns here, so spawned task is shut down as it sleeps.
});
```

See the guide on [Scopes](https://frontside.com/effection/docs/scope) for
more detail.

If a `Task` is halted before it finishes executing, then consuming it's
result is an Error.

---
#### Example 4
```javascript
import { run, spawn, sleep } from "effection";

await run(function*() {
 let task = yield* spawn(function*() {
   yield* sleep(100);
   return "hello world";
 });
 yield* task.halt();
 let output = yield* task; //=> throws Error("halted");
 console.log(output);
});
```

---


### Type Parameters
T


### Methods
<dl>
<dt class="border-dotted [&:not(:first-child)]:border-t-2 [&:not(:first-child)]:pt-3 [&:not(:first-child)]:mt-2"><h4 id="halt" class="inline scroll-mt-[100px]">halt</h4>(): [Future](https://frontside.com/effection/api/v3/Future.md)&lt;void&gt;</dt>
<dd class="flex flex-col [&>pre]:mb-3 [&>p:not(:first-child)]:mt-0" >
Interrupt and shut down a running [Operation](https://frontside.com/effection/api/v3/Operation.md) and all of its
children.

Any errors raised by the `halt()` operation only represent problems that
occured during the teardown of the task. In other words, `halt()` can
succeed even if the task failed.

</dd>
</dl>


### See
* [run](https://frontside.com/effection/api/v3/run.md)
* [spawn](https://frontside.com/effection/api/v3/spawn.md)
* [Scope.run](https://frontside.com/effection/api/v3/Scope.md)

[View code](https://github.com/thefrontside/effection/tree/effection-v3.6.1/lib/types.ts#L136)
