diff --git a/doc/api/process.md b/doc/api/process.md index 28db286bd90fd2..5d042977f96307 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1817,6 +1817,71 @@ a code. Specifying a code to [`process.exit(code)`][`process.exit()`] will override any previous setting of `process.exitCode`. +## `process.getActiveResourcesInfo()` + + + +> Stability: 1 - Experimental + +* Returns: {Array} of objects containing the `type`, `asyncId` and the + `triggerAsyncId` of the currently active requests, handles and timers. + +The `process.getActiveResourcesInfo()` method can be called at any moment and it +would return an array of objects containing some information about all the +resources that are currently keeping the event loop alive. This can be useful +while debugging. + +The same functionality can be implemented using [`async_hooks`][] by setting up +an [`AsyncHook`][] that keeps a track of the calls made to [`init` callbacks][] +and [`destroy` callbacks][] but this function should be preferred because it +doesn't incur the overhead of creating any [`AsyncHook`][]. + +```mjs +import { getActiveResourcesInfo } from 'process'; +import { setTimeout } from 'timers'; + +console.log('Before:', getActiveResourcesInfo()); +setTimeout(() => {}, 1000); +console.log('After:', getActiveResourcesInfo()); +// Prints: +// Before: [ +// { type: 'CloseReq', asyncId: 6, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 7, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 9, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 10, triggerAsyncId: 0 } +// ] +// After: [ +// { type: 'CloseReq', asyncId: 6, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 7, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 9, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 10, triggerAsyncId: 0 }, +// { type: 'Timeout', asyncId: 12, triggerAsyncId: 0 } +// ] +``` + +```cjs +const { getActiveResourcesInfo } = require('process'); +const { setTimeout } = require('timers'); + +console.log('Before:', getActiveResourcesInfo()); +setTimeout(() => {}, 1000); +console.log('After:', getActiveResourcesInfo()); +// Prints: +// Before: [ +// { type: 'TTYWrap', asyncId: 2, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 4, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 5, triggerAsyncId: 1 } +// ] +// After: [ +// { type: 'TTYWrap', asyncId: 2, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 4, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 5, triggerAsyncId: 1 }, +// { type: 'Timeout', asyncId: 7, triggerAsyncId: 1 } +// ] +``` + ## `process.getegid()`