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 call
thefrontside/effectionfunction call<T>(callable: () => Operation<T>): Operation<T>
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
let response = yield* call(fetch('https://google.com'));
or an async function:
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
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
yield* call(() => "a string");
Because call()
runs within its own Scope, it can also be used to
establish .
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!
}
}
Type Parameters
T
Parameters
callable: () => Operation<T>
the operation, promise, async function, generator funnction, or plain function to call as part of this operation
Return Type
Operation<T>