Effection Logo

function spawn

thefrontside/effection

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

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 https://effection-www-s5a0jkwj0z5x.deno.dev/api/v3/Scope 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

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: () => https://effection-www-s5a0jkwj0z5x.deno.dev/api/v3/Operation&lt;T>

the operation to run as a child of the current task

Return Type

https://effection-www-s5a0jkwj0z5x.deno.dev/api/v3/Operation&lt;https://effection-www-s5a0jkwj0z5x.deno.dev/api/v3/Task&lt;T>>

a https://effection-www-s5a0jkwj0z5x.deno.dev/api/v3/Task representing a handle to the running operation