import { call } from "https://deno.land/x/effection@3.0.3/lib/mod.ts";
Pause the current operation, then runs a promise, async function, plain function, or operation within a new scope. The calling operation will be resumed (or errored) once call is completed.
call()
is a uniform integration point for calling async functions,
evaluating promises, generator functions, operations, and plain
functions.
It can be used to treat a promise as an operation:
Examples
Example 1
Example 1
let response = yield* call(fetch('https://google.com'));
or an async function:
Example 2
Example 2
async function* googleSlowly() {
return yield* call(async function() {
await new Promise(resolve => setTimeout(resolve, 2000));
return await fetch("https://google.com");
});
}
It can be used to run an operation in a separate scope to ensure that any resources allocated will be cleaned up:
Example 3
Example 3
yield* call(function*() {
let socket = yield* useSocket();
return yield* socket.read();
}); // => socket is destroyed before returning
It can be used to run a plain function:
Example 4
Example 4
yield* call(() => "a string");
Because call()
runs within its own Scope, it can also be used to
establish * establish error boundaries https://frontside.com/effection/docs/errors | error boundaries.
Example 5
Example 5
function* myop() {
let task = yield* spawn(function*() {
throw new Error("boom!");
});
yield* task;
}
function* runner() {
try {
yield* myop();
} catch (err) {
// this will never get hit!
}
}
function* runner() {
try {
yield* call(myop);
} catch(err) {
// properly catches `spawn` errors!
}
}