Skip to content

uhyo/async-object-stack

Repository files navigation

async-object-stack

A wrapper of AsyncLocalStorage that keeps a stack of objects. Supports the using syntax to automatically pop objects from the stack when the current scope ends.

Primary use case is creating a nice API for structural logging. See Structural Logging Example.

Reference | Zenn Article (日本語)

Installation

npm install async-object-stack

Example

See also: Structural Logging Example.

import { createAsyncObjectStack } from 'async-object-stack';

const stack = createAsyncObjectStack();

console.log(stack.render()); // {}
using guard = stack.push({ pika: "chu" });
console.log(stack.render()); // { pika: "chu" }
{
  using guard2 = stack.push({ bulba: "saur" });
  console.log(stack.render()); // { pika: "chu", bulba: "saur" }
}
console.log(stack.render()); // { pika: "chu" }

License

MIT