Effection Logo

function call

thefrontside/effection

function call<T>(callable: () => Operation<T>): Operation<T>

Pause the current operation, then run an async function, or operation function in 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, and generator functions.

It can be used to invoke an async function:

Examples

Example 1

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 2

yield* call(function*() {
  let socket = yield* useSocket();
  return yield* socket.read();
}); // => socket is destroyed before returning

Because call() runs within its own Scope, it can also be used to establish error boundaries.


Example 3

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>

function call<T>(callable: () => Promise<T>): Operation<T>

Type Parameters

T

Parameters

callable: () => Promise<T>

Return Type

Operation<T>

function call<T>(callable: () => T): Operation<T>

Deprecated

Using call with simple functions will be removed in v4. To convert simple functions into operations, use lift.

Type Parameters

T

Parameters

callable: () => T

Return Type

Operation<T>

function call<T>(callable: Operation<T>): Operation<T>

Deprecated

calling bare promises, operations, and constants will be removed in v4, always pass a function to call()

before: call(operation); after: call(() => operation);

Type Parameters

T

Parameters

callable: Operation<T>

Return Type

Operation<T>

function call<T>(callable: Promise<T>): Operation<T>

Deprecated

calling bare promises, operations, and constants will be removed in v4, always pass a function to call()

before: call(promise); after: call(() => promise);

Type Parameters

T

Parameters

callable: Promise<T>

Return Type

Operation<T>

function call<T>(callable: Callable<T>): Operation<T>

Type Parameters

T

Parameters

callable: Callable<T>

Return Type

Operation<T>