Effection Logo

function createScope

thefrontside/effection

function createScope(): [Scope, () => Future<void>]

Create a new Scope as a child of parent, inheriting all its contexts. along with a method to destroy the scope. Whenever the scope is destroyd, all tasks and resources it contains will be halted.

This function is used mostly by frameworks as an intergration point to enter Effection.

Examples

Example 1

import { createScope, sleep, suspend } from "effection";

let [scope, destroy] = createScope();

let delay = scope.run(function*() {
  yield* sleep(1000);
});
scope.run(function*() {
  try {
    yield* suspend();
   } finally {
     console.log('done!');
   }
});
await delay;
await destroy(); // prints "done!";

Parameters

scope. If no parent is specified it will derive directly from global

Return Type

[Scope, () => Future<void>]

a tuple containing the freshly created scope, along with a function to destroy it.