method
diagnostics_channel.BoundedChannel.withScope
context: ContextType
Create a disposable scope for tracing a synchronous operation using JavaScript's explicit resource management (using syntax). The scope automatically publishes start and end events, enters bound stores, and handles cleanup when disposed.
import { boundedChannel } from 'node:diagnostics_channel';
const wc = boundedChannel('my-operation');
const context = { operationId: '123' };
{
using scope = wc.withScope(context);
// Stores are entered, start event is published
// Perform work and set result on context
context.result = 42;
}
// End event is published, stores are restored automatically@param context
Shared object to correlate events through
@returns
Disposable scope object
Referenced types
interface BoundedChannelScope
The class BoundedChannelScope represents a disposable scope created by boundedChannel.withScope(context). It manages the lifecycle of a traced operation, automatically publishing events and managing store contexts.
The scope must be used with the using syntax to ensure proper disposal.
import { boundedChannel } from 'node:diagnostics_channel';
const wc = boundedChannel('my-operation');
const context = {};
{
using scope = wc.withScope(context);
// Start event is published, stores are entered
context.result = performOperation();
// End event is automatically published at end of block
}