Create a scope exit handle that will call the provided callback when disposed, to be used with using or await using.
using
await using
For example, this code:
import {scopeExit} from "lifecycle-utils";function example() { using exitHandle = scopeExit(() => { console.log("exiting scope"); }); console.log("inside scope");}async function asyncExample() { await using exitHandle = scopeExit(async () => { await new Promise((resolve) => setTimeout(resolve, 100)); console.log("exiting async scope"); }); console.log("inside async scope");}example();console.log("example done");console.log()await asyncExample();console.log("asyncExample done"); Copy
import {scopeExit} from "lifecycle-utils";function example() { using exitHandle = scopeExit(() => { console.log("exiting scope"); }); console.log("inside scope");}async function asyncExample() { await using exitHandle = scopeExit(async () => { await new Promise((resolve) => setTimeout(resolve, 100)); console.log("exiting async scope"); }); console.log("inside async scope");}example();console.log("example done");console.log()await asyncExample();console.log("asyncExample done");
Will print this:
inside scopeexiting scopeexample doneinside async scopeexiting async scopeasyncExample done Copy
inside scopeexiting scopeexample doneinside async scopeexiting async scopeasyncExample done
Create a scope exit handle that will call the provided callback when disposed, to be used with
usingorawait using.For example, this code:
Will print this: