Newer version available
You’re viewing the API reference for version 3.0.3, but the latest version is 3.1.0. The latest version include may important updates and fixes.
View Latest Versionfunction spawn
thefrontside/effectionfunction 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
Scope.run 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: () => Operation<T>
the operation to run as a child of the current task
Return Type
a Task representing a handle to the running operation