Only optional in child class.
The unique asyncId assigned to the resource.
The returned AsyncResource object has an additional eventEmitter property
that provides a reference to this EventEmitterAsyncResource.
The same triggerAsyncId that is passed to the AsyncResource constructor.
Call all destroy hooks. This should only ever be called once. An error will
be thrown if it is called more than once. This must be manually called. If
the resource is left to be collected by the GC then the destroy hooks will
never be called.
Generated using TypeDoc
Integrates
EventEmitterwithAsyncResourceforEventEmitters that require manual async tracking. Specifically, all events emitted by instances ofevents.EventEmitterAsyncResourcewill run within itsasync context.import { EventEmitterAsyncResource, EventEmitter } from 'node:events'; import { notStrictEqual, strictEqual } from 'node:assert'; import { executionAsyncId, triggerAsyncId } from 'node:async_hooks'; // Async tracking tooling will identify this as 'Q'. const ee1 = new EventEmitterAsyncResource({ name: 'Q' }); // 'foo' listeners will run in the EventEmitters async context. ee1.on('foo', () => { strictEqual(executionAsyncId(), ee1.asyncId); strictEqual(triggerAsyncId(), ee1.triggerAsyncId); }); const ee2 = new EventEmitter(); // 'foo' listeners on ordinary EventEmitters that do not track async // context, however, run in the same async context as the emit(). ee2.on('foo', () => { notStrictEqual(executionAsyncId(), ee2.asyncId); notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId); }); Promise.resolve().then(() => { ee1.emit('foo'); ee2.emit('foo'); });The
EventEmitterAsyncResourceclass has the same methods and takes the same options asEventEmitterandAsyncResourcethemselves.v17.4.0, v16.14.0