import { spawn } from "https://deno.land/x/effection@3.0.0-beta.3/mod.ts";
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.
});