• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.18.2">
7  <title>Async hooks | Node.js v18.18.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/async_hooks.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:606px){.with-48-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:542px){.with-40-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1072px){.with-72-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1048px){.with-69-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1144px){.with-81-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:638px){.with-52-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1080px){.with-73-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1176px){.with-85-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-async_hooks">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks active">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="async_hooks" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.18.2 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.18.2</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_1"><a href="#async-hooks">Async hooks</a></span>
124<ul>
125<li><a href="#terminology">Terminology</a></li>
126<li><a href="#overview">Overview</a></li>
127<li><a href="#async_hookscreatehookcallbacks"><code>async_hooks.createHook(callbacks)</code></a>
128<ul>
129<li><a href="#error-handling">Error handling</a></li>
130<li><a href="#printing-in-asynchook-callbacks">Printing in <code>AsyncHook</code> callbacks</a></li>
131</ul>
132</li>
133<li><a href="#class-asynchook">Class: <code>AsyncHook</code></a>
134<ul>
135<li><a href="#asynchookenable"><code>asyncHook.enable()</code></a></li>
136<li><a href="#asynchookdisable"><code>asyncHook.disable()</code></a></li>
137<li><a href="#hook-callbacks">Hook callbacks</a>
138<ul>
139<li><a href="#initasyncid-type-triggerasyncid-resource"><code>init(asyncId, type, triggerAsyncId, resource)</code></a>
140<ul>
141<li><a href="#type"><code>type</code></a></li>
142<li><a href="#triggerasyncid"><code>triggerAsyncId</code></a></li>
143<li><a href="#resource"><code>resource</code></a></li>
144<li><a href="#asynchronous-context-example">Asynchronous context example</a></li>
145</ul>
146</li>
147<li><a href="#beforeasyncid"><code>before(asyncId)</code></a></li>
148<li><a href="#afterasyncid"><code>after(asyncId)</code></a></li>
149<li><a href="#destroyasyncid"><code>destroy(asyncId)</code></a></li>
150<li><a href="#promiseresolveasyncid"><code>promiseResolve(asyncId)</code></a></li>
151</ul>
152</li>
153<li><a href="#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a></li>
154<li><a href="#async_hooksexecutionasyncid"><code>async_hooks.executionAsyncId()</code></a></li>
155<li><a href="#async_hookstriggerasyncid"><code>async_hooks.triggerAsyncId()</code></a></li>
156<li><a href="#async_hooksasyncwrapproviders"><code>async_hooks.asyncWrapProviders</code></a></li>
157</ul>
158</li>
159<li><a href="#promise-execution-tracking">Promise execution tracking</a></li>
160<li><a href="#javascript-embedder-api">JavaScript embedder API</a>
161<ul>
162<li><a href="#class-asyncresource">Class: <code>AsyncResource</code></a></li>
163</ul>
164</li>
165<li><a href="#class-asynclocalstorage">Class: <code>AsyncLocalStorage</code></a></li>
166</ul>
167</li>
168</ul></div></div>
169    </li>
170
171
172    <li class="picker-header">
173      <a href="#">
174        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
175        Index
176      </a>
177
178      <div class="picker"><ul>
179<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
180<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
181
182      <li>
183        <a href="index.html">Index</a>
184      </li>
185    </ul>
186
187<hr class="line">
188<ul>
189<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
190<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
191<li><a href="async_hooks.html" class="nav-async_hooks active">Async hooks</a></li>
192<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
193<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
194<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
195<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
196<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
197<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
198<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
199<li><a href="console.html" class="nav-console">Console</a></li>
200<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
201<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
202<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
203<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
204<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
205<li><a href="dns.html" class="nav-dns">DNS</a></li>
206<li><a href="domain.html" class="nav-domain">Domain</a></li>
207<li><a href="errors.html" class="nav-errors">Errors</a></li>
208<li><a href="events.html" class="nav-events">Events</a></li>
209<li><a href="fs.html" class="nav-fs">File system</a></li>
210<li><a href="globals.html" class="nav-globals">Globals</a></li>
211<li><a href="http.html" class="nav-http">HTTP</a></li>
212<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
213<li><a href="https.html" class="nav-https">HTTPS</a></li>
214<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
215<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
216<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
217<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
218<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
219<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
220<li><a href="net.html" class="nav-net">Net</a></li>
221<li><a href="os.html" class="nav-os">OS</a></li>
222<li><a href="path.html" class="nav-path">Path</a></li>
223<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
224<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
225<li><a href="process.html" class="nav-process">Process</a></li>
226<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
227<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
228<li><a href="readline.html" class="nav-readline">Readline</a></li>
229<li><a href="repl.html" class="nav-repl">REPL</a></li>
230<li><a href="report.html" class="nav-report">Report</a></li>
231<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
232<li><a href="stream.html" class="nav-stream">Stream</a></li>
233<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
234<li><a href="test.html" class="nav-test">Test runner</a></li>
235<li><a href="timers.html" class="nav-timers">Timers</a></li>
236<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
237<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
238<li><a href="tty.html" class="nav-tty">TTY</a></li>
239<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
240<li><a href="url.html" class="nav-url">URL</a></li>
241<li><a href="util.html" class="nav-util">Utilities</a></li>
242<li><a href="v8.html" class="nav-v8">V8</a></li>
243<li><a href="vm.html" class="nav-vm">VM</a></li>
244<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
245<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
246<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
247<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
248<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
249</ul>
250<hr class="line">
251<ul>
252<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
253</ul></div>
254    </li>
255
256
257    <li class="picker-header">
258      <a href="#">
259        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
260        Other versions
261      </a>
262      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/async_hooks.html">20.x</a></li>
263<li><a href="https://nodejs.org/docs/latest-v19.x/api/async_hooks.html">19.x</a></li>
264<li><a href="https://nodejs.org/docs/latest-v18.x/api/async_hooks.html">18.x <b>LTS</b></a></li>
265<li><a href="https://nodejs.org/docs/latest-v17.x/api/async_hooks.html">17.x</a></li>
266<li><a href="https://nodejs.org/docs/latest-v16.x/api/async_hooks.html">16.x</a></li>
267<li><a href="https://nodejs.org/docs/latest-v15.x/api/async_hooks.html">15.x</a></li>
268<li><a href="https://nodejs.org/docs/latest-v14.x/api/async_hooks.html">14.x</a></li>
269<li><a href="https://nodejs.org/docs/latest-v13.x/api/async_hooks.html">13.x</a></li>
270<li><a href="https://nodejs.org/docs/latest-v12.x/api/async_hooks.html">12.x</a></li>
271<li><a href="https://nodejs.org/docs/latest-v11.x/api/async_hooks.html">11.x</a></li>
272<li><a href="https://nodejs.org/docs/latest-v10.x/api/async_hooks.html">10.x</a></li>
273<li><a href="https://nodejs.org/docs/latest-v9.x/api/async_hooks.html">9.x</a></li>
274<li><a href="https://nodejs.org/docs/latest-v8.x/api/async_hooks.html">8.x</a></li></ol></div>
275    </li>
276
277            <li class="picker-header">
278              <a href="#">
279                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
280                Options
281              </a>
282
283              <div class="picker">
284                <ul>
285                  <li>
286                    <a href="all.html">View on single page</a>
287                  </li>
288                  <li>
289                    <a href="async_hooks.json">View as JSON</a>
290                  </li>
291                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/async_hooks.md">Edit on GitHub</a></li>
292                </ul>
293              </div>
294            </li>
295          </ul>
296        </div>
297        <hr>
298      </header>
299
300      <details id="toc" open><summary>Table of contents</summary><ul>
301<li><span class="stability_1"><a href="#async-hooks">Async hooks</a></span>
302<ul>
303<li><a href="#terminology">Terminology</a></li>
304<li><a href="#overview">Overview</a></li>
305<li><a href="#async_hookscreatehookcallbacks"><code>async_hooks.createHook(callbacks)</code></a>
306<ul>
307<li><a href="#error-handling">Error handling</a></li>
308<li><a href="#printing-in-asynchook-callbacks">Printing in <code>AsyncHook</code> callbacks</a></li>
309</ul>
310</li>
311<li><a href="#class-asynchook">Class: <code>AsyncHook</code></a>
312<ul>
313<li><a href="#asynchookenable"><code>asyncHook.enable()</code></a></li>
314<li><a href="#asynchookdisable"><code>asyncHook.disable()</code></a></li>
315<li><a href="#hook-callbacks">Hook callbacks</a>
316<ul>
317<li><a href="#initasyncid-type-triggerasyncid-resource"><code>init(asyncId, type, triggerAsyncId, resource)</code></a>
318<ul>
319<li><a href="#type"><code>type</code></a></li>
320<li><a href="#triggerasyncid"><code>triggerAsyncId</code></a></li>
321<li><a href="#resource"><code>resource</code></a></li>
322<li><a href="#asynchronous-context-example">Asynchronous context example</a></li>
323</ul>
324</li>
325<li><a href="#beforeasyncid"><code>before(asyncId)</code></a></li>
326<li><a href="#afterasyncid"><code>after(asyncId)</code></a></li>
327<li><a href="#destroyasyncid"><code>destroy(asyncId)</code></a></li>
328<li><a href="#promiseresolveasyncid"><code>promiseResolve(asyncId)</code></a></li>
329</ul>
330</li>
331<li><a href="#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a></li>
332<li><a href="#async_hooksexecutionasyncid"><code>async_hooks.executionAsyncId()</code></a></li>
333<li><a href="#async_hookstriggerasyncid"><code>async_hooks.triggerAsyncId()</code></a></li>
334<li><a href="#async_hooksasyncwrapproviders"><code>async_hooks.asyncWrapProviders</code></a></li>
335</ul>
336</li>
337<li><a href="#promise-execution-tracking">Promise execution tracking</a></li>
338<li><a href="#javascript-embedder-api">JavaScript embedder API</a>
339<ul>
340<li><a href="#class-asyncresource">Class: <code>AsyncResource</code></a></li>
341</ul>
342</li>
343<li><a href="#class-asynclocalstorage">Class: <code>AsyncLocalStorage</code></a></li>
344</ul>
345</li>
346</ul></details>
347
348      <div id="apicontent">
349        <h2>Async hooks<span><a class="mark" href="#async-hooks" id="async-hooks">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_async_hooks"></a></h2>
350
351<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental. Please migrate away from this API, if you can.
352We do not recommend using the <a href="#async_hookscreatehookcallbacks"><code>createHook</code></a>, <a href="#class-asynchook"><code>AsyncHook</code></a>, and
353<a href="#async_hooksexecutionasyncresource"><code>executionAsyncResource</code></a> APIs as they have usability issues, safety risks,
354and performance implications. Async context tracking use cases are better
355served by the stable <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a> API. If you have a use case for
356<code>createHook</code>, <code>AsyncHook</code>, or <code>executionAsyncResource</code> beyond the context
357tracking need solved by <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a> or diagnostics data currently
358provided by <a href="diagnostics_channel.html">Diagnostics Channel</a>, please open an issue at
359<a href="https://github.com/nodejs/node/issues">https://github.com/nodejs/node/issues</a> describing your use case so we can
360create a more purpose-focused API.</div><p></p>
361<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/async_hooks.js">lib/async_hooks.js</a></p>
362<p>We strongly discourage the use of the <code>async_hooks</code> API.
363Other APIs that can cover most of its use cases include:</p>
364<ul>
365<li><a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a> tracks async context</li>
366<li><a href="process.html#processgetactiveresourcesinfo"><code>process.getActiveResourcesInfo()</code></a> tracks active resources</li>
367</ul>
368<p>The <code>node:async_hooks</code> module provides an API to track asynchronous resources.
369It can be accessed using:</p>
370
371<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> async_hooks <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);</code><button class="copy-button">copy</button></pre>
372<section><h3>Terminology<span><a class="mark" href="#terminology" id="terminology">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_terminology"></a></h3>
373<p>An asynchronous resource represents an object with an associated callback.
374This callback may be called multiple times, such as the <code>'connection'</code>
375event in <code>net.createServer()</code>, or just a single time like in <code>fs.open()</code>.
376A resource can also be closed before the callback is called. <code>AsyncHook</code> does
377not explicitly distinguish between these different cases but will represent them
378as the abstract concept that is a resource.</p>
379<p>If <a href="worker_threads.html#class-worker"><code>Worker</code></a>s are used, each thread has an independent <code>async_hooks</code>
380interface, and each thread will use a new set of async IDs.</p>
381</section><section><h3>Overview<span><a class="mark" href="#overview" id="overview">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_overview"></a></h3>
382<p>Following is a simple overview of the public API.</p>
383
384<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> async_hooks <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
385
386<span class="hljs-comment">// Return the ID of the current execution context.</span>
387<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
388
389<span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback of the</span>
390<span class="hljs-comment">// current execution scope to call.</span>
391<span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
392
393<span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.</span>
394<span class="hljs-keyword">const</span> asyncHook =
395    async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, promiseResolve });
396
397<span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an implicit</span>
398<span class="hljs-comment">// action after running the constructor, and must be explicitly run to begin</span>
399<span class="hljs-comment">// executing callbacks.</span>
400asyncHook.<span class="hljs-title function_">enable</span>();
401
402<span class="hljs-comment">// Disable listening for new asynchronous events.</span>
403asyncHook.<span class="hljs-title function_">disable</span>();
404
405<span class="hljs-comment">//</span>
406<span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</span>
407<span class="hljs-comment">//</span>
408
409<span class="hljs-comment">// init() is called during object construction. The resource may not have</span>
410<span class="hljs-comment">// completed construction when this callback runs. Therefore, all fields of the</span>
411<span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
412<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { }
413
414<span class="hljs-comment">// before() is called just before the resource's callback is called. It can be</span>
415<span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called exactly 1</span>
416<span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
417<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
418
419<span class="hljs-comment">// after() is called just after the resource's callback has finished.</span>
420<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
421
422<span class="hljs-comment">// destroy() is called when the resource is destroyed.</span>
423<span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
424
425<span class="hljs-comment">// promiseResolve() is called only for promise resources, when the</span>
426<span class="hljs-comment">// resolve() function passed to the Promise constructor is invoked</span>
427<span class="hljs-comment">// (either directly or through other means of resolving a promise).</span>
428<span class="hljs-keyword">function</span> <span class="hljs-title function_">promiseResolve</span>(<span class="hljs-params">asyncId</span>) { }</code><code class="language-js cjs"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
429
430<span class="hljs-comment">// Return the ID of the current execution context.</span>
431<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
432
433<span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback of the</span>
434<span class="hljs-comment">// current execution scope to call.</span>
435<span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
436
437<span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.</span>
438<span class="hljs-keyword">const</span> asyncHook =
439    async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, promiseResolve });
440
441<span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an implicit</span>
442<span class="hljs-comment">// action after running the constructor, and must be explicitly run to begin</span>
443<span class="hljs-comment">// executing callbacks.</span>
444asyncHook.<span class="hljs-title function_">enable</span>();
445
446<span class="hljs-comment">// Disable listening for new asynchronous events.</span>
447asyncHook.<span class="hljs-title function_">disable</span>();
448
449<span class="hljs-comment">//</span>
450<span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</span>
451<span class="hljs-comment">//</span>
452
453<span class="hljs-comment">// init() is called during object construction. The resource may not have</span>
454<span class="hljs-comment">// completed construction when this callback runs. Therefore, all fields of the</span>
455<span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
456<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { }
457
458<span class="hljs-comment">// before() is called just before the resource's callback is called. It can be</span>
459<span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called exactly 1</span>
460<span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
461<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
462
463<span class="hljs-comment">// after() is called just after the resource's callback has finished.</span>
464<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
465
466<span class="hljs-comment">// destroy() is called when the resource is destroyed.</span>
467<span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
468
469<span class="hljs-comment">// promiseResolve() is called only for promise resources, when the</span>
470<span class="hljs-comment">// resolve() function passed to the Promise constructor is invoked</span>
471<span class="hljs-comment">// (either directly or through other means of resolving a promise).</span>
472<span class="hljs-keyword">function</span> <span class="hljs-title function_">promiseResolve</span>(<span class="hljs-params">asyncId</span>) { }</code><button class="copy-button">copy</button></pre>
473</section><section><h3><code>async_hooks.createHook(callbacks)</code><span><a class="mark" href="#async_hookscreatehookcallbacks" id="async_hookscreatehookcallbacks">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_async_hooks_createhook_callbacks"></a></h3>
474<div class="api_metadata">
475<span>Added in: v8.1.0</span>
476</div>
477<ul>
478<li><code>callbacks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <a href="#hook-callbacks">Hook Callbacks</a> to register
479<ul>
480<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#initasyncid-type-triggerasyncid-resource"><code>init</code> callback</a>.</li>
481<li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#beforeasyncid"><code>before</code> callback</a>.</li>
482<li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#afterasyncid"><code>after</code> callback</a>.</li>
483<li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#destroyasyncid"><code>destroy</code> callback</a>.</li>
484<li><code>promiseResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The <a href="#promiseresolveasyncid"><code>promiseResolve</code> callback</a>.</li>
485</ul>
486</li>
487<li>Returns: <a href="async_hooks.html#async_hookscreatehookcallbacks" class="type">&#x3C;AsyncHook></a> Instance used for disabling and enabling hooks</li>
488</ul>
489<p>Registers functions to be called for different lifetime events of each async
490operation.</p>
491<p>The callbacks <code>init()</code>/<code>before()</code>/<code>after()</code>/<code>destroy()</code> are called for the
492respective asynchronous event during a resource's lifetime.</p>
493<p>All callbacks are optional. For example, if only resource cleanup needs to
494be tracked, then only the <code>destroy</code> callback needs to be passed. The
495specifics of all functions that can be passed to <code>callbacks</code> is in the
496<a href="#hook-callbacks">Hook Callbacks</a> section.</p>
497
498<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createHook } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
499
500<span class="hljs-keyword">const</span> asyncHook = <span class="hljs-title function_">createHook</span>({
501  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { },
502  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { },
503});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
504
505<span class="hljs-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">createHook</span>({
506  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { },
507  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { },
508});</code><button class="copy-button">copy</button></pre>
509<p>The callbacks will be inherited via the prototype chain:</p>
510<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyAsyncCallbacks</span> {
511  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { }
512  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {}
513}
514
515<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyAddedCallbacks</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">MyAsyncCallbacks</span> {
516  <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
517  <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
518}
519
520<span class="hljs-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">createHook</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">MyAddedCallbacks</span>());</code> <button class="copy-button">copy</button></pre>
521<p>Because promises are asynchronous resources whose lifecycle is tracked
522via the async hooks mechanism, the <code>init()</code>, <code>before()</code>, <code>after()</code>, and
523<code>destroy()</code> callbacks <em>must not</em> be async functions that return promises.</p>
524<h4>Error handling<span><a class="mark" href="#error-handling" id="error-handling">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_error_handling"></a></h4>
525<p>If any <code>AsyncHook</code> callbacks throw, the application will print the stack trace
526and exit. The exit path does follow that of an uncaught exception, but
527all <code>'uncaughtException'</code> listeners are removed, thus forcing the process to
528exit. The <code>'exit'</code> callbacks will still be called unless the application is run
529with <code>--abort-on-uncaught-exception</code>, in which case a stack trace will be
530printed and the application exits, leaving a core file.</p>
531<p>The reason for this error handling behavior is that these callbacks are running
532at potentially volatile points in an object's lifetime, for example during
533class construction and destruction. Because of this, it is deemed necessary to
534bring down the process quickly in order to prevent an unintentional abort in the
535future. This is subject to change in the future if a comprehensive analysis is
536performed to ensure an exception can follow the normal control flow without
537unintentional side effects.</p>
538<h4>Printing in <code>AsyncHook</code> callbacks<span><a class="mark" href="#printing-in-asynchook-callbacks" id="printing-in-asynchook-callbacks">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_printing_in_asynchook_callbacks"></a></h4>
539<p>Because printing to the console is an asynchronous operation, <code>console.log()</code>
540will cause <code>AsyncHook</code> callbacks to be called. Using <code>console.log()</code> or
541similar asynchronous operations inside an <code>AsyncHook</code> callback function will
542cause an infinite recursion. An easy solution to this when debugging is to use a
543synchronous logging operation such as <code>fs.writeFileSync(file, msg, flag)</code>.
544This will print to the file and will not invoke <code>AsyncHook</code> recursively because
545it is synchronous.</p>
546
547<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
548<span class="hljs-keyword">import</span> { format } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>;
549
550<span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span class="hljs-params">...args</span>) {
551  <span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHook callback</span>
552  <span class="hljs-title function_">writeFileSync</span>(<span class="hljs-string">'log.out'</span>, <span class="hljs-string">`<span class="hljs-subst">${format(...args)}</span>\n`</span>, { <span class="hljs-attr">flag</span>: <span class="hljs-string">'a'</span> });
553}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
554<span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>);
555
556<span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span class="hljs-params">...args</span>) {
557  <span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHook callback</span>
558  fs.<span class="hljs-title function_">writeFileSync</span>(<span class="hljs-string">'log.out'</span>, <span class="hljs-string">`<span class="hljs-subst">${util.format(...args)}</span>\n`</span>, { <span class="hljs-attr">flag</span>: <span class="hljs-string">'a'</span> });
559}</code><button class="copy-button">copy</button></pre>
560<p>If an asynchronous operation is needed for logging, it is possible to keep
561track of what caused the asynchronous operation using the information
562provided by <code>AsyncHook</code> itself. The logging should then be skipped when
563it was the logging itself that caused the <code>AsyncHook</code> callback to be called. By
564doing this, the otherwise infinite recursion is broken.</p>
565</section><section><h3>Class: <code>AsyncHook</code><span><a class="mark" href="#class-asynchook" id="class-asynchook">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_class_asynchook"></a></h3>
566<p>The class <code>AsyncHook</code> exposes an interface for tracking lifetime events
567of asynchronous operations.</p>
568<h4><code>asyncHook.enable()</code><span><a class="mark" href="#asynchookenable" id="asynchookenable">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_asynchook_enable"></a></h4>
569<ul>
570<li>Returns: <a href="async_hooks.html#async_hookscreatehookcallbacks" class="type">&#x3C;AsyncHook></a> A reference to <code>asyncHook</code>.</li>
571</ul>
572<p>Enable the callbacks for a given <code>AsyncHook</code> instance. If no callbacks are
573provided, enabling is a no-op.</p>
574<p>The <code>AsyncHook</code> instance is disabled by default. If the <code>AsyncHook</code> instance
575should be enabled immediately after creation, the following pattern can be used.</p>
576
577<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createHook } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
578
579<span class="hljs-keyword">const</span> hook = <span class="hljs-title function_">createHook</span>(callbacks).<span class="hljs-title function_">enable</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
580
581<span class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">createHook</span>(callbacks).<span class="hljs-title function_">enable</span>();</code><button class="copy-button">copy</button></pre>
582<h4><code>asyncHook.disable()</code><span><a class="mark" href="#asynchookdisable" id="asynchookdisable">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_asynchook_disable"></a></h4>
583<ul>
584<li>Returns: <a href="async_hooks.html#async_hookscreatehookcallbacks" class="type">&#x3C;AsyncHook></a> A reference to <code>asyncHook</code>.</li>
585</ul>
586<p>Disable the callbacks for a given <code>AsyncHook</code> instance from the global pool of
587<code>AsyncHook</code> callbacks to be executed. Once a hook has been disabled it will not
588be called again until enabled.</p>
589<p>For API consistency <code>disable()</code> also returns the <code>AsyncHook</code> instance.</p>
590<h4>Hook callbacks<span><a class="mark" href="#hook-callbacks" id="hook-callbacks">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_hook_callbacks"></a></h4>
591<p>Key events in the lifetime of asynchronous events have been categorized into
592four areas: instantiation, before/after the callback is called, and when the
593instance is destroyed.</p>
594<h5><code>init(asyncId, type, triggerAsyncId, resource)</code><span><a class="mark" href="#initasyncid-type-triggerasyncid-resource" id="initasyncid-type-triggerasyncid-resource">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_init_asyncid_type_triggerasyncid_resource"></a></h5>
595<ul>
596<li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> A unique ID for the async resource.</li>
597<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The type of the async resource.</li>
598<li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The unique ID of the async resource in whose
599execution context this async resource was created.</li>
600<li><code>resource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Reference to the resource representing the async
601operation, needs to be released during <em>destroy</em>.</li>
602</ul>
603<p>Called when a class is constructed that has the <em>possibility</em> to emit an
604asynchronous event. This <em>does not</em> mean the instance must call
605<code>before</code>/<code>after</code> before <code>destroy</code> is called, only that the possibility
606exists.</p>
607<p>This behavior can be observed by doing something like opening a resource then
608closing it before the resource can be used. The following snippet demonstrates
609this.</p>
610
611<pre class="with-72-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
612
613<span class="hljs-title function_">createServer</span>().<span class="hljs-title function_">listen</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(); });
614<span class="hljs-comment">// OR</span>
615<span class="hljs-built_in">clearTimeout</span>(<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {}, <span class="hljs-number">10</span>));</code><code class="language-js cjs"><span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>).<span class="hljs-title function_">createServer</span>().<span class="hljs-title function_">listen</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(); });
616<span class="hljs-comment">// OR</span>
617<span class="hljs-built_in">clearTimeout</span>(<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {}, <span class="hljs-number">10</span>));</code><button class="copy-button">copy</button></pre>
618<p>Every new resource is assigned an ID that is unique within the scope of the
619current Node.js instance.</p>
620<h6><code>type</code><span><a class="mark" href="#type" id="type">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_type"></a></h6>
621<p>The <code>type</code> is a string identifying the type of resource that caused
622<code>init</code> to be called. Generally, it will correspond to the name of the
623resource's constructor.</p>
624<p>The <code>type</code> of resources created by Node.js itself can change in any Node.js
625release. Valid values include <code>TLSWRAP</code>,
626<code>TCPWRAP</code>, <code>TCPSERVERWRAP</code>, <code>GETADDRINFOREQWRAP</code>, <code>FSREQCALLBACK</code>,
627<code>Microtask</code>, and <code>Timeout</code>. Inspect the source code of the Node.js version used
628to get the full list.</p>
629<p>Furthermore users of <a href="async_context.html#class-asyncresource"><code>AsyncResource</code></a> create async resources independent
630of Node.js itself.</p>
631<p>There is also the <code>PROMISE</code> resource type, which is used to track <code>Promise</code>
632instances and asynchronous work scheduled by them.</p>
633<p>Users are able to define their own <code>type</code> when using the public embedder API.</p>
634<p>It is possible to have type name collisions. Embedders are encouraged to use
635unique prefixes, such as the npm package name, to prevent collisions when
636listening to the hooks.</p>
637<h6><code>triggerAsyncId</code><span><a class="mark" href="#triggerasyncid" id="triggerasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_triggerasyncid"></a></h6>
638<p><code>triggerAsyncId</code> is the <code>asyncId</code> of the resource that caused (or "triggered")
639the new resource to initialize and that caused <code>init</code> to call. This is different
640from <code>async_hooks.executionAsyncId()</code> that only shows <em>when</em> a resource was
641created, while <code>triggerAsyncId</code> shows <em>why</em> a resource was created.</p>
642<p>The following is a simple demonstration of <code>triggerAsyncId</code>:</p>
643
644<pre class="with-69-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createHook, executionAsyncId } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
645<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
646<span class="hljs-keyword">import</span> net <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
647<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
648
649<span class="hljs-title function_">createHook</span>({
650  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
651    <span class="hljs-keyword">const</span> eid = <span class="hljs-title function_">executionAsyncId</span>();
652    fs.<span class="hljs-title function_">writeSync</span>(
653      stdout.<span class="hljs-property">fd</span>,
654      <span class="hljs-string">`<span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>): trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: <span class="hljs-subst">${eid}</span>\n`</span>);
655  },
656}).<span class="hljs-title function_">enable</span>();
657
658net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn</span>) =></span> {}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createHook, executionAsyncId } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
659<span class="hljs-keyword">const</span> { stdout } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>);
660<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
661<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
662
663<span class="hljs-title function_">createHook</span>({
664  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
665    <span class="hljs-keyword">const</span> eid = <span class="hljs-title function_">executionAsyncId</span>();
666    fs.<span class="hljs-title function_">writeSync</span>(
667      stdout.<span class="hljs-property">fd</span>,
668      <span class="hljs-string">`<span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>): trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: <span class="hljs-subst">${eid}</span>\n`</span>);
669  },
670}).<span class="hljs-title function_">enable</span>();
671
672net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn</span>) =></span> {}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>);</code><button class="copy-button">copy</button></pre>
673<p>Output when hitting the server with <code>nc localhost 8080</code>:</p>
674<pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
675TCPWRAP(7): trigger: 5 execution: 0</code> <button class="copy-button">copy</button></pre>
676<p>The <code>TCPSERVERWRAP</code> is the server which receives the connections.</p>
677<p>The <code>TCPWRAP</code> is the new connection from the client. When a new
678connection is made, the <code>TCPWrap</code> instance is immediately constructed. This
679happens outside of any JavaScript stack. (An <code>executionAsyncId()</code> of <code>0</code> means
680that it is being executed from C++ with no JavaScript stack above it.) With only
681that information, it would be impossible to link resources together in
682terms of what caused them to be created, so <code>triggerAsyncId</code> is given the task
683of propagating what resource is responsible for the new resource's existence.</p>
684<h6><code>resource</code><span><a class="mark" href="#resource" id="resource">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_resource"></a></h6>
685<p><code>resource</code> is an object that represents the actual async resource that has
686been initialized. The API to access the object may be specified by the
687creator of the resource. Resources created by Node.js itself are internal
688and may change at any time. Therefore no API is specified for these.</p>
689<p>In some cases the resource object is reused for performance reasons, it is
690thus not safe to use it as a key in a <code>WeakMap</code> or add properties to it.</p>
691<h6>Asynchronous context example<span><a class="mark" href="#asynchronous-context-example" id="asynchronous-context-example">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_asynchronous_context_example"></a></h6>
692<p>The context tracking use case is covered by the stable API <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.
693This example only illustrates async hooks operation but <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>
694fits better to this use case.</p>
695<p>The following is an example with additional information about the calls to
696<code>init</code> between the <code>before</code> and <code>after</code> calls, specifically what the
697callback to <code>listen()</code> will look like. The output formatting is slightly more
698elaborate to make calling context easier to see.</p>
699
700<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> async_hooks <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
701<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
702<span class="hljs-keyword">import</span> net <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
703<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
704<span class="hljs-keyword">const</span> { fd } = stdout;
705
706<span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
707async_hooks.<span class="hljs-title function_">createHook</span>({
708  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
709    <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
710    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
711    fs.<span class="hljs-title function_">writeSync</span>(
712      fd,
713      <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>):`</span> +
714      <span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: <span class="hljs-subst">${eid}</span>\n`</span>);
715  },
716  <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
717    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
718    fs.<span class="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span>before:  <span class="hljs-subst">${asyncId}</span>\n`</span>);
719    indent += <span class="hljs-number">2</span>;
720  },
721  <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
722    indent -= <span class="hljs-number">2</span>;
723    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
724    fs.<span class="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span>after:  <span class="hljs-subst">${asyncId}</span>\n`</span>);
725  },
726  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
727    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
728    fs.<span class="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span>destroy:  <span class="hljs-subst">${asyncId}</span>\n`</span>);
729  },
730}).<span class="hljs-title function_">enable</span>();
731
732net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">() =></span> {}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>, <span class="hljs-function">() =></span> {
733  <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
734  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
735    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'>>>'</span>, async_hooks.<span class="hljs-title function_">executionAsyncId</span>());
736  }, <span class="hljs-number">10</span>);
737});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
738<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
739<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
740<span class="hljs-keyword">const</span> { fd } = process.<span class="hljs-property">stdout</span>;
741
742<span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
743async_hooks.<span class="hljs-title function_">createHook</span>({
744  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
745    <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
746    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
747    fs.<span class="hljs-title function_">writeSync</span>(
748      fd,
749      <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>):`</span> +
750      <span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: <span class="hljs-subst">${eid}</span>\n`</span>);
751  },
752  <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
753    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
754    fs.<span class="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span>before:  <span class="hljs-subst">${asyncId}</span>\n`</span>);
755    indent += <span class="hljs-number">2</span>;
756  },
757  <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
758    indent -= <span class="hljs-number">2</span>;
759    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
760    fs.<span class="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span>after:  <span class="hljs-subst">${asyncId}</span>\n`</span>);
761  },
762  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
763    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
764    fs.<span class="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span>destroy:  <span class="hljs-subst">${asyncId}</span>\n`</span>);
765  },
766}).<span class="hljs-title function_">enable</span>();
767
768net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">() =></span> {}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>, <span class="hljs-function">() =></span> {
769  <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
770  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
771    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'>>>'</span>, async_hooks.<span class="hljs-title function_">executionAsyncId</span>());
772  }, <span class="hljs-number">10</span>);
773});</code><button class="copy-button">copy</button></pre>
774<p>Output from only starting the server:</p>
775<pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
776TickObject(6): trigger: 5 execution: 1
777before:  6
778  Timeout(7): trigger: 6 execution: 6
779after:   6
780destroy: 6
781before:  7
782<span class="hljs-meta prompt_">></span><span class="language-bash">>> 7</span>
783  TickObject(8): trigger: 7 execution: 7
784after:   7
785before:  8
786after:   8</code> <button class="copy-button">copy</button></pre>
787<p>As illustrated in the example, <code>executionAsyncId()</code> and <code>execution</code> each specify
788the value of the current execution context; which is delineated by calls to
789<code>before</code> and <code>after</code>.</p>
790<p>Only using <code>execution</code> to graph resource allocation results in the following:</p>
791<pre><code class="language-console">  root(1)
792     ^
793     |
794TickObject(6)
795     ^
796     |
797 Timeout(7)</code> <button class="copy-button">copy</button></pre>
798<p>The <code>TCPSERVERWRAP</code> is not part of this graph, even though it was the reason for
799<code>console.log()</code> being called. This is because binding to a port without a host
800name is a <em>synchronous</em> operation, but to maintain a completely asynchronous
801API the user's callback is placed in a <code>process.nextTick()</code>. Which is why
802<code>TickObject</code> is present in the output and is a 'parent' for <code>.listen()</code>
803callback.</p>
804<p>The graph only shows <em>when</em> a resource was created, not <em>why</em>, so to track
805the <em>why</em> use <code>triggerAsyncId</code>. Which can be represented with the following
806graph:</p>
807<pre><code class="language-console"> bootstrap(1)
808     |
809     ˅
810TCPSERVERWRAP(5)
811     |
812     ˅
813 TickObject(6)
814     |
815     ˅
816  Timeout(7)</code> <button class="copy-button">copy</button></pre>
817<h5><code>before(asyncId)</code><span><a class="mark" href="#beforeasyncid" id="beforeasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_before_asyncid"></a></h5>
818<ul>
819<li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
820</ul>
821<p>When an asynchronous operation is initiated (such as a TCP server receiving a
822new connection) or completes (such as writing data to disk) a callback is
823called to notify the user. The <code>before</code> callback is called just before said
824callback is executed. <code>asyncId</code> is the unique identifier assigned to the
825resource about to execute the callback.</p>
826<p>The <code>before</code> callback will be called 0 to N times. The <code>before</code> callback
827will typically be called 0 times if the asynchronous operation was cancelled
828or, for example, if no connections are received by a TCP server. Persistent
829asynchronous resources like a TCP server will typically call the <code>before</code>
830callback multiple times, while other operations like <code>fs.open()</code> will call
831it only once.</p>
832<h5><code>after(asyncId)</code><span><a class="mark" href="#afterasyncid" id="afterasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_after_asyncid"></a></h5>
833<ul>
834<li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
835</ul>
836<p>Called immediately after the callback specified in <code>before</code> is completed.</p>
837<p>If an uncaught exception occurs during execution of the callback, then <code>after</code>
838will run <em>after</em> the <code>'uncaughtException'</code> event is emitted or a <code>domain</code>'s
839handler runs.</p>
840<h5><code>destroy(asyncId)</code><span><a class="mark" href="#destroyasyncid" id="destroyasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_destroy_asyncid"></a></h5>
841<ul>
842<li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
843</ul>
844<p>Called after the resource corresponding to <code>asyncId</code> is destroyed. It is also
845called asynchronously from the embedder API <code>emitDestroy()</code>.</p>
846<p>Some resources depend on garbage collection for cleanup, so if a reference is
847made to the <code>resource</code> object passed to <code>init</code> it is possible that <code>destroy</code>
848will never be called, causing a memory leak in the application. If the resource
849does not depend on garbage collection, then this will not be an issue.</p>
850<p>Using the destroy hook results in additional overhead because it enables
851tracking of <code>Promise</code> instances via the garbage collector.</p>
852<h5><code>promiseResolve(asyncId)</code><span><a class="mark" href="#promiseresolveasyncid" id="promiseresolveasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_promiseresolve_asyncid"></a></h5>
853<div class="api_metadata">
854<span>Added in: v8.6.0</span>
855</div>
856<ul>
857<li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
858</ul>
859<p>Called when the <code>resolve</code> function passed to the <code>Promise</code> constructor is
860invoked (either directly or through other means of resolving a promise).</p>
861<p><code>resolve()</code> does not do any observable synchronous work.</p>
862<p>The <code>Promise</code> is not necessarily fulfilled or rejected at this point if the
863<code>Promise</code> was resolved by assuming the state of another <code>Promise</code>.</p>
864<pre><code class="language-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =></span> <span class="hljs-title function_">resolve</span>(<span class="hljs-literal">true</span>)).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">a</span>) =></span> {});</code> <button class="copy-button">copy</button></pre>
865<p>calls the following callbacks:</p>
866<pre><code class="language-text">init for PROMISE with id 5, trigger id: 1
867  promise resolve 5      # corresponds to resolve(true)
868init for PROMISE with id 6, trigger id: 5  # the Promise returned by then()
869  before 6               # the then() callback is entered
870  promise resolve 6      # the then() callback resolves the promise by returning
871  after 6</code> <button class="copy-button">copy</button></pre>
872<h4><code>async_hooks.executionAsyncResource()</code><span><a class="mark" href="#async_hooksexecutionasyncresource" id="async_hooksexecutionasyncresource">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_async_hooks_executionasyncresource"></a></h4>
873<div class="api_metadata">
874<span>Added in: v13.9.0, v12.17.0</span>
875</div>
876<ul>
877<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The resource representing the current execution.
878Useful to store data within the resource.</li>
879</ul>
880<p>Resource objects returned by <code>executionAsyncResource()</code> are most often internal
881Node.js handle objects with undocumented APIs. Using any functions or properties
882on the object is likely to crash your application and should be avoided.</p>
883<p>Using <code>executionAsyncResource()</code> in the top-level execution context will
884return an empty object as there is no handle or request object to use,
885but having an object representing the top-level can be helpful.</p>
886
887<pre class="with-81-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
888<span class="hljs-keyword">import</span> { executionAsyncId, executionAsyncResource } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
889
890<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">executionAsyncId</span>(), <span class="hljs-title function_">executionAsyncResource</span>());  <span class="hljs-comment">// 1 {}</span>
891<span class="hljs-title function_">open</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>), <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
892  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">executionAsyncId</span>(), <span class="hljs-title function_">executionAsyncResource</span>());  <span class="hljs-comment">// 7 FSReqWrap</span>
893});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { open } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
894<span class="hljs-keyword">const</span> { executionAsyncId, executionAsyncResource } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
895
896<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">executionAsyncId</span>(), <span class="hljs-title function_">executionAsyncResource</span>());  <span class="hljs-comment">// 1 {}</span>
897<span class="hljs-title function_">open</span>(__filename, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
898  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">executionAsyncId</span>(), <span class="hljs-title function_">executionAsyncResource</span>());  <span class="hljs-comment">// 7 FSReqWrap</span>
899});</code><button class="copy-button">copy</button></pre>
900<p>This can be used to implement continuation local storage without the
901use of a tracking <code>Map</code> to store the metadata:</p>
902
903<pre class="with-46-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createServer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>;
904<span class="hljs-keyword">import</span> {
905  executionAsyncId,
906  executionAsyncResource,
907  createHook,
908} <span class="hljs-keyword">from</span> <span class="hljs-string">'async_hooks'</span>;
909<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'state'</span>); <span class="hljs-comment">// Private symbol to avoid pollution</span>
910
911<span class="hljs-title function_">createHook</span>({
912  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) {
913    <span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncResource</span>();
914    <span class="hljs-keyword">if</span> (cr) {
915      resource[sym] = cr[sym];
916    }
917  },
918}).<span class="hljs-title function_">enable</span>();
919
920<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
921  <span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr">state</span>: req.<span class="hljs-property">url</span> };
922  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {
923    res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(<span class="hljs-title function_">executionAsyncResource</span>()[sym]));
924  }, <span class="hljs-number">100</span>);
925}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>);
926<span class="hljs-keyword">const</span> {
927  executionAsyncId,
928  executionAsyncResource,
929  createHook,
930} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
931<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'state'</span>); <span class="hljs-comment">// Private symbol to avoid pollution</span>
932
933<span class="hljs-title function_">createHook</span>({
934  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) {
935    <span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncResource</span>();
936    <span class="hljs-keyword">if</span> (cr) {
937      resource[sym] = cr[sym];
938    }
939  },
940}).<span class="hljs-title function_">enable</span>();
941
942<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
943  <span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr">state</span>: req.<span class="hljs-property">url</span> };
944  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {
945    res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(<span class="hljs-title function_">executionAsyncResource</span>()[sym]));
946  }, <span class="hljs-number">100</span>);
947}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><button class="copy-button">copy</button></pre>
948<h4><code>async_hooks.executionAsyncId()</code><span><a class="mark" href="#async_hooksexecutionasyncid" id="async_hooksexecutionasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_async_hooks_executionasyncid"></a></h4>
949<div class="api_metadata">
950<details class="changelog"><summary>History</summary>
951<table>
952<tbody><tr><th>Version</th><th>Changes</th></tr>
953<tr><td>v8.2.0</td>
954<td><p>Renamed from <code>currentId</code>.</p></td></tr>
955<tr><td>v8.1.0</td>
956<td><p><span>Added in: v8.1.0</span></p></td></tr>
957</tbody></table>
958</details>
959</div>
960<ul>
961<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The <code>asyncId</code> of the current execution context. Useful to
962track when something calls.</li>
963</ul>
964
965<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { executionAsyncId } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
966<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
967
968<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">executionAsyncId</span>());  <span class="hljs-comment">// 1 - bootstrap</span>
969fs.<span class="hljs-title function_">open</span>(path, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
970  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">executionAsyncId</span>());  <span class="hljs-comment">// 6 - open()</span>
971});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
972<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
973
974<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<span class="hljs-title function_">executionAsyncId</span>());  <span class="hljs-comment">// 1 - bootstrap</span>
975fs.<span class="hljs-title function_">open</span>(path, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
976  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<span class="hljs-title function_">executionAsyncId</span>());  <span class="hljs-comment">// 6 - open()</span>
977});</code><button class="copy-button">copy</button></pre>
978<p>The ID returned from <code>executionAsyncId()</code> is related to execution timing, not
979causality (which is covered by <code>triggerAsyncId()</code>):</p>
980<pre><code class="language-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn</span>) =></span> {
981  <span class="hljs-comment">// Returns the ID of the server, not of the new connection, because the</span>
982  <span class="hljs-comment">// callback runs in the execution scope of the server's MakeCallback().</span>
983  async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
984
985}).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
986  <span class="hljs-comment">// Returns the ID of a TickObject (process.nextTick()) because all</span>
987  <span class="hljs-comment">// callbacks passed to .listen() are wrapped in a nextTick().</span>
988  async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
989});</code> <button class="copy-button">copy</button></pre>
990<p>Promise contexts may not get precise <code>executionAsyncIds</code> by default.
991See the section on <a href="#promise-execution-tracking">promise execution tracking</a>.</p>
992<h4><code>async_hooks.triggerAsyncId()</code><span><a class="mark" href="#async_hookstriggerasyncid" id="async_hookstriggerasyncid">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_async_hooks_triggerasyncid"></a></h4>
993<ul>
994<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The ID of the resource responsible for calling the callback
995that is currently being executed.</li>
996</ul>
997<pre><code class="language-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn</span>) =></span> {
998  <span class="hljs-comment">// The resource that caused (or triggered) this callback to be called</span>
999  <span class="hljs-comment">// was that of the new connection. Thus the return value of triggerAsyncId()</span>
1000  <span class="hljs-comment">// is the asyncId of "conn".</span>
1001  async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
1002
1003}).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
1004  <span class="hljs-comment">// Even though all callbacks passed to .listen() are wrapped in a nextTick()</span>
1005  <span class="hljs-comment">// the callback itself exists because the call to the server's .listen()</span>
1006  <span class="hljs-comment">// was made. So the return value would be the ID of the server.</span>
1007  async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
1008});</code> <button class="copy-button">copy</button></pre>
1009<p>Promise contexts may not get valid <code>triggerAsyncId</code>s by default. See
1010the section on <a href="#promise-execution-tracking">promise execution tracking</a>.</p>
1011<h4><code>async_hooks.asyncWrapProviders</code><span><a class="mark" href="#async_hooksasyncwrapproviders" id="async_hooksasyncwrapproviders">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_async_hooks_asyncwrapproviders"></a></h4>
1012<div class="api_metadata">
1013<span>Added in: v17.2.0, v16.14.0</span>
1014</div>
1015<ul>
1016<li>Returns: A map of provider types to the corresponding numeric id.
1017This map contains all the event types that might be emitted by the <code>async_hooks.init()</code> event.</li>
1018</ul>
1019<p>This feature suppresses the deprecated usage of <code>process.binding('async_wrap').Providers</code>.
1020See: <a href="deprecations.html#dep0111-processbinding">DEP0111</a></p>
1021</section><section><h3>Promise execution tracking<span><a class="mark" href="#promise-execution-tracking" id="promise-execution-tracking">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_promise_execution_tracking"></a></h3>
1022<p>By default, promise executions are not assigned <code>asyncId</code>s due to the relatively
1023expensive nature of the <a href="https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit">promise introspection API</a> provided by
1024V8. This means that programs using promises or <code>async</code>/<code>await</code> will not get
1025correct execution and trigger ids for promise callback contexts by default.</p>
1026
1027<pre class="with-73-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { executionAsyncId, triggerAsyncId } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
1028
1029<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">1729</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
1030  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`eid <span class="hljs-subst">${executionAsyncId()}</span> tid <span class="hljs-subst">${triggerAsyncId()}</span>`</span>);
1031});
1032<span class="hljs-comment">// produces:</span>
1033<span class="hljs-comment">// eid 1 tid 0</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { executionAsyncId, triggerAsyncId } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
1034
1035<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">1729</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
1036  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`eid <span class="hljs-subst">${executionAsyncId()}</span> tid <span class="hljs-subst">${triggerAsyncId()}</span>`</span>);
1037});
1038<span class="hljs-comment">// produces:</span>
1039<span class="hljs-comment">// eid 1 tid 0</span></code><button class="copy-button">copy</button></pre>
1040<p>Observe that the <code>then()</code> callback claims to have executed in the context of the
1041outer scope even though there was an asynchronous hop involved. Also,
1042the <code>triggerAsyncId</code> value is <code>0</code>, which means that we are missing context about
1043the resource that caused (triggered) the <code>then()</code> callback to be executed.</p>
1044<p>Installing async hooks via <code>async_hooks.createHook</code> enables promise execution
1045tracking:</p>
1046
1047<pre class="with-85-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createHook, executionAsyncId, triggerAsyncId } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
1048<span class="hljs-title function_">createHook</span>({ <span class="hljs-title function_">init</span>(<span class="hljs-params"></span>) {} }).<span class="hljs-title function_">enable</span>(); <span class="hljs-comment">// forces PromiseHooks to be enabled.</span>
1049<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">1729</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
1050  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`eid <span class="hljs-subst">${executionAsyncId()}</span> tid <span class="hljs-subst">${triggerAsyncId()}</span>`</span>);
1051});
1052<span class="hljs-comment">// produces:</span>
1053<span class="hljs-comment">// eid 7 tid 6</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createHook, executionAsyncId, triggerAsyncId } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
1054
1055<span class="hljs-title function_">createHook</span>({ <span class="hljs-title function_">init</span>(<span class="hljs-params"></span>) {} }).<span class="hljs-title function_">enable</span>(); <span class="hljs-comment">// forces PromiseHooks to be enabled.</span>
1056<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">1729</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
1057  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`eid <span class="hljs-subst">${executionAsyncId()}</span> tid <span class="hljs-subst">${triggerAsyncId()}</span>`</span>);
1058});
1059<span class="hljs-comment">// produces:</span>
1060<span class="hljs-comment">// eid 7 tid 6</span></code><button class="copy-button">copy</button></pre>
1061<p>In this example, adding any actual hook function enabled the tracking of
1062promises. There are two promises in the example above; the promise created by
1063<code>Promise.resolve()</code> and the promise returned by the call to <code>then()</code>. In the
1064example above, the first promise got the <code>asyncId</code> <code>6</code> and the latter got
1065<code>asyncId</code> <code>7</code>. During the execution of the <code>then()</code> callback, we are executing
1066in the context of promise with <code>asyncId</code> <code>7</code>. This promise was triggered by
1067async resource <code>6</code>.</p>
1068<p>Another subtlety with promises is that <code>before</code> and <code>after</code> callbacks are run
1069only on chained promises. That means promises not created by <code>then()</code>/<code>catch()</code>
1070will not have the <code>before</code> and <code>after</code> callbacks fired on them. For more details
1071see the details of the V8 <a href="https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit">PromiseHooks</a> API.</p>
1072</section><section><h3>JavaScript embedder API<span><a class="mark" href="#javascript-embedder-api" id="javascript-embedder-api">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_javascript_embedder_api"></a></h3>
1073<p>Library developers that handle their own asynchronous resources performing tasks
1074like I/O, connection pooling, or managing callback queues may use the
1075<code>AsyncResource</code> JavaScript API so that all the appropriate callbacks are called.</p>
1076<h4>Class: <code>AsyncResource</code><span><a class="mark" href="#class-asyncresource" id="class-asyncresource">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_class_asyncresource"></a></h4>
1077<p>The documentation for this class has moved <a href="async_context.html#class-asyncresource"><code>AsyncResource</code></a>.</p>
1078</section><section><h3>Class: <code>AsyncLocalStorage</code><span><a class="mark" href="#class-asynclocalstorage" id="class-asynclocalstorage">#</a></span><a aria-hidden="true" class="legacy" id="async_hooks_class_asynclocalstorage"></a></h3>
1079<p>The documentation for this class has moved <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.</p></section>
1080        <!-- API END -->
1081      </div>
1082    </div>
1083  </div>
1084</body>
1085</html>
1086