# spawn

```ts
function spawn<T>(operation: () => Operation<T>): Operation<Task<T>>
```

Run another operation concurrently as a child of the current one.

The spawned operation will begin executing immediately and control will
return to the caller when it reaches its first suspend point.

### Example

```typescript
import { main, sleep, suspend, spawn } from 'effection';

await main(function*() {
  yield* spawn(function*() {
    yield* sleep(1000);
    console.log("hello");
  });
  yield* spawn(function*() {
    yield* sleep(2000);
    console.log("world");
  });
  yield* suspend();
});
```

You should prefer using the spawn operation over calling
[Scope.run](https://frontside.com/effection/api/v3/Scope.md) from within Effection code. The reason being that a
synchronous failure in the spawned operation will not be caught
until the next yield point when using `run`, which results in lines
being executed that should not.

### Example

```typescript
import { main, suspend, spawn, useScope } from 'effection';

await main(function*() {
  yield* useScope();

  scope.run(function*() {
   throw new Error('boom!');
  });

  console.log('this code will run and probably should not');

  yield* suspend(); // <- error is thrown after this.
});
```
### Type Parameters
T
the type that the spawned task evaluates to


### Parameters


**operation**: () => [Operation](https://frontside.com/effection/api/v3/Operation.md)&lt;T&gt;


the operation to run as a child of the current task
### Return Type


[Operation](https://frontside.com/effection/api/v3/Operation.md)&lt;[Task](https://frontside.com/effection/api/v3/Task.md)&lt;T&gt;&gt;


a [Task](https://frontside.com/effection/api/v3/Task.md) representing a handle to the running operation

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