11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>Async hooks | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/async_hooks.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  <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>
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-async_hooks">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks active">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="async_hooks" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#async-hooks">Async hooks</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#terminology">Terminology</a></li>
1261cb0ef41Sopenharmony_ci<li><a href="#overview">Overview</a></li>
1271cb0ef41Sopenharmony_ci<li><a href="#async_hookscreatehookcallbacks"><code>async_hooks.createHook(callbacks)</code></a>
1281cb0ef41Sopenharmony_ci<ul>
1291cb0ef41Sopenharmony_ci<li><a href="#error-handling">Error handling</a></li>
1301cb0ef41Sopenharmony_ci<li><a href="#printing-in-asynchook-callbacks">Printing in <code>AsyncHook</code> callbacks</a></li>
1311cb0ef41Sopenharmony_ci</ul>
1321cb0ef41Sopenharmony_ci</li>
1331cb0ef41Sopenharmony_ci<li><a href="#class-asynchook">Class: <code>AsyncHook</code></a>
1341cb0ef41Sopenharmony_ci<ul>
1351cb0ef41Sopenharmony_ci<li><a href="#asynchookenable"><code>asyncHook.enable()</code></a></li>
1361cb0ef41Sopenharmony_ci<li><a href="#asynchookdisable"><code>asyncHook.disable()</code></a></li>
1371cb0ef41Sopenharmony_ci<li><a href="#hook-callbacks">Hook callbacks</a>
1381cb0ef41Sopenharmony_ci<ul>
1391cb0ef41Sopenharmony_ci<li><a href="#initasyncid-type-triggerasyncid-resource"><code>init(asyncId, type, triggerAsyncId, resource)</code></a>
1401cb0ef41Sopenharmony_ci<ul>
1411cb0ef41Sopenharmony_ci<li><a href="#type"><code>type</code></a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#triggerasyncid"><code>triggerAsyncId</code></a></li>
1431cb0ef41Sopenharmony_ci<li><a href="#resource"><code>resource</code></a></li>
1441cb0ef41Sopenharmony_ci<li><a href="#asynchronous-context-example">Asynchronous context example</a></li>
1451cb0ef41Sopenharmony_ci</ul>
1461cb0ef41Sopenharmony_ci</li>
1471cb0ef41Sopenharmony_ci<li><a href="#beforeasyncid"><code>before(asyncId)</code></a></li>
1481cb0ef41Sopenharmony_ci<li><a href="#afterasyncid"><code>after(asyncId)</code></a></li>
1491cb0ef41Sopenharmony_ci<li><a href="#destroyasyncid"><code>destroy(asyncId)</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#promiseresolveasyncid"><code>promiseResolve(asyncId)</code></a></li>
1511cb0ef41Sopenharmony_ci</ul>
1521cb0ef41Sopenharmony_ci</li>
1531cb0ef41Sopenharmony_ci<li><a href="#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a></li>
1541cb0ef41Sopenharmony_ci<li><a href="#async_hooksexecutionasyncid"><code>async_hooks.executionAsyncId()</code></a></li>
1551cb0ef41Sopenharmony_ci<li><a href="#async_hookstriggerasyncid"><code>async_hooks.triggerAsyncId()</code></a></li>
1561cb0ef41Sopenharmony_ci<li><a href="#async_hooksasyncwrapproviders"><code>async_hooks.asyncWrapProviders</code></a></li>
1571cb0ef41Sopenharmony_ci</ul>
1581cb0ef41Sopenharmony_ci</li>
1591cb0ef41Sopenharmony_ci<li><a href="#promise-execution-tracking">Promise execution tracking</a></li>
1601cb0ef41Sopenharmony_ci<li><a href="#javascript-embedder-api">JavaScript embedder API</a>
1611cb0ef41Sopenharmony_ci<ul>
1621cb0ef41Sopenharmony_ci<li><a href="#class-asyncresource">Class: <code>AsyncResource</code></a></li>
1631cb0ef41Sopenharmony_ci</ul>
1641cb0ef41Sopenharmony_ci</li>
1651cb0ef41Sopenharmony_ci<li><a href="#class-asynclocalstorage">Class: <code>AsyncLocalStorage</code></a></li>
1661cb0ef41Sopenharmony_ci</ul>
1671cb0ef41Sopenharmony_ci</li>
1681cb0ef41Sopenharmony_ci</ul></div></div>
1691cb0ef41Sopenharmony_ci    </li>
1701cb0ef41Sopenharmony_ci  
1711cb0ef41Sopenharmony_ci            
1721cb0ef41Sopenharmony_ci    <li class="picker-header">
1731cb0ef41Sopenharmony_ci      <a href="#">
1741cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1751cb0ef41Sopenharmony_ci        Index
1761cb0ef41Sopenharmony_ci      </a>
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci      <div class="picker"><ul>
1791cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
1801cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci      <li>
1831cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
1841cb0ef41Sopenharmony_ci      </li>
1851cb0ef41Sopenharmony_ci    </ul>
1861cb0ef41Sopenharmony_ci  
1871cb0ef41Sopenharmony_ci<hr class="line">
1881cb0ef41Sopenharmony_ci<ul>
1891cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
1901cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
1911cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks active">Async hooks</a></li>
1921cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
1931cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
1941cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
1951cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
1961cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
1971cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
1981cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
1991cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
2001cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
2011cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
2021cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
2031cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
2041cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
2051cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
2061cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
2071cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
2081cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
2091cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
2101cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
2111cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
2121cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
2131cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
2141cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
2151cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
2161cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
2171cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
2181cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
2191cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
2201cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
2211cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
2221cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
2231cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
2241cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
2251cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
2261cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
2271cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
2281cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
2291cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
2301cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
2311cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
2331cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
2341cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
2351cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
2361cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
2371cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
2381cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
2391cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
2401cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
2411cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
2421cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
2431cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
2441cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
2451cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
2461cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
2471cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
2481cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
2491cb0ef41Sopenharmony_ci</ul>
2501cb0ef41Sopenharmony_ci<hr class="line">
2511cb0ef41Sopenharmony_ci<ul>
2521cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
2531cb0ef41Sopenharmony_ci</ul></div>
2541cb0ef41Sopenharmony_ci    </li>
2551cb0ef41Sopenharmony_ci  
2561cb0ef41Sopenharmony_ci            
2571cb0ef41Sopenharmony_ci    <li class="picker-header">
2581cb0ef41Sopenharmony_ci      <a href="#">
2591cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2601cb0ef41Sopenharmony_ci        Other versions
2611cb0ef41Sopenharmony_ci      </a>
2621cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/async_hooks.html">21.x</a></li>
2631cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/async_hooks.html">20.x <b>LTS</b></a></li>
2641cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/async_hooks.html">19.x</a></li>
2651cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/async_hooks.html">18.x <b>LTS</b></a></li>
2661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/async_hooks.html">17.x</a></li>
2671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/async_hooks.html">16.x</a></li>
2681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/async_hooks.html">15.x</a></li>
2691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/async_hooks.html">14.x</a></li>
2701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/async_hooks.html">13.x</a></li>
2711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/async_hooks.html">12.x</a></li>
2721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/async_hooks.html">11.x</a></li>
2731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/async_hooks.html">10.x</a></li>
2741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/async_hooks.html">9.x</a></li>
2751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/async_hooks.html">8.x</a></li></ol></div>
2761cb0ef41Sopenharmony_ci    </li>
2771cb0ef41Sopenharmony_ci  
2781cb0ef41Sopenharmony_ci            <li class="picker-header">
2791cb0ef41Sopenharmony_ci              <a href="#">
2801cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2811cb0ef41Sopenharmony_ci                Options
2821cb0ef41Sopenharmony_ci              </a>
2831cb0ef41Sopenharmony_ci        
2841cb0ef41Sopenharmony_ci              <div class="picker">
2851cb0ef41Sopenharmony_ci                <ul>
2861cb0ef41Sopenharmony_ci                  <li>
2871cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
2881cb0ef41Sopenharmony_ci                  </li>
2891cb0ef41Sopenharmony_ci                  <li>
2901cb0ef41Sopenharmony_ci                    <a href="async_hooks.json">View as JSON</a>
2911cb0ef41Sopenharmony_ci                  </li>
2921cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/async_hooks.md">Edit on GitHub</a></li>    
2931cb0ef41Sopenharmony_ci                </ul>
2941cb0ef41Sopenharmony_ci              </div>
2951cb0ef41Sopenharmony_ci            </li>
2961cb0ef41Sopenharmony_ci          </ul>
2971cb0ef41Sopenharmony_ci        </div>
2981cb0ef41Sopenharmony_ci        <hr>
2991cb0ef41Sopenharmony_ci      </header>
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
3021cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#async-hooks">Async hooks</a></span>
3031cb0ef41Sopenharmony_ci<ul>
3041cb0ef41Sopenharmony_ci<li><a href="#terminology">Terminology</a></li>
3051cb0ef41Sopenharmony_ci<li><a href="#overview">Overview</a></li>
3061cb0ef41Sopenharmony_ci<li><a href="#async_hookscreatehookcallbacks"><code>async_hooks.createHook(callbacks)</code></a>
3071cb0ef41Sopenharmony_ci<ul>
3081cb0ef41Sopenharmony_ci<li><a href="#error-handling">Error handling</a></li>
3091cb0ef41Sopenharmony_ci<li><a href="#printing-in-asynchook-callbacks">Printing in <code>AsyncHook</code> callbacks</a></li>
3101cb0ef41Sopenharmony_ci</ul>
3111cb0ef41Sopenharmony_ci</li>
3121cb0ef41Sopenharmony_ci<li><a href="#class-asynchook">Class: <code>AsyncHook</code></a>
3131cb0ef41Sopenharmony_ci<ul>
3141cb0ef41Sopenharmony_ci<li><a href="#asynchookenable"><code>asyncHook.enable()</code></a></li>
3151cb0ef41Sopenharmony_ci<li><a href="#asynchookdisable"><code>asyncHook.disable()</code></a></li>
3161cb0ef41Sopenharmony_ci<li><a href="#hook-callbacks">Hook callbacks</a>
3171cb0ef41Sopenharmony_ci<ul>
3181cb0ef41Sopenharmony_ci<li><a href="#initasyncid-type-triggerasyncid-resource"><code>init(asyncId, type, triggerAsyncId, resource)</code></a>
3191cb0ef41Sopenharmony_ci<ul>
3201cb0ef41Sopenharmony_ci<li><a href="#type"><code>type</code></a></li>
3211cb0ef41Sopenharmony_ci<li><a href="#triggerasyncid"><code>triggerAsyncId</code></a></li>
3221cb0ef41Sopenharmony_ci<li><a href="#resource"><code>resource</code></a></li>
3231cb0ef41Sopenharmony_ci<li><a href="#asynchronous-context-example">Asynchronous context example</a></li>
3241cb0ef41Sopenharmony_ci</ul>
3251cb0ef41Sopenharmony_ci</li>
3261cb0ef41Sopenharmony_ci<li><a href="#beforeasyncid"><code>before(asyncId)</code></a></li>
3271cb0ef41Sopenharmony_ci<li><a href="#afterasyncid"><code>after(asyncId)</code></a></li>
3281cb0ef41Sopenharmony_ci<li><a href="#destroyasyncid"><code>destroy(asyncId)</code></a></li>
3291cb0ef41Sopenharmony_ci<li><a href="#promiseresolveasyncid"><code>promiseResolve(asyncId)</code></a></li>
3301cb0ef41Sopenharmony_ci</ul>
3311cb0ef41Sopenharmony_ci</li>
3321cb0ef41Sopenharmony_ci<li><a href="#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a></li>
3331cb0ef41Sopenharmony_ci<li><a href="#async_hooksexecutionasyncid"><code>async_hooks.executionAsyncId()</code></a></li>
3341cb0ef41Sopenharmony_ci<li><a href="#async_hookstriggerasyncid"><code>async_hooks.triggerAsyncId()</code></a></li>
3351cb0ef41Sopenharmony_ci<li><a href="#async_hooksasyncwrapproviders"><code>async_hooks.asyncWrapProviders</code></a></li>
3361cb0ef41Sopenharmony_ci</ul>
3371cb0ef41Sopenharmony_ci</li>
3381cb0ef41Sopenharmony_ci<li><a href="#promise-execution-tracking">Promise execution tracking</a></li>
3391cb0ef41Sopenharmony_ci<li><a href="#javascript-embedder-api">JavaScript embedder API</a>
3401cb0ef41Sopenharmony_ci<ul>
3411cb0ef41Sopenharmony_ci<li><a href="#class-asyncresource">Class: <code>AsyncResource</code></a></li>
3421cb0ef41Sopenharmony_ci</ul>
3431cb0ef41Sopenharmony_ci</li>
3441cb0ef41Sopenharmony_ci<li><a href="#class-asynclocalstorage">Class: <code>AsyncLocalStorage</code></a></li>
3451cb0ef41Sopenharmony_ci</ul>
3461cb0ef41Sopenharmony_ci</li>
3471cb0ef41Sopenharmony_ci</ul></details>
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci      <div id="apicontent">
3501cb0ef41Sopenharmony_ci        <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>
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ci<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.
3531cb0ef41Sopenharmony_ciWe do not recommend using the <a href="#async_hookscreatehookcallbacks"><code>createHook</code></a>, <a href="#class-asynchook"><code>AsyncHook</code></a>, and
3541cb0ef41Sopenharmony_ci<a href="#async_hooksexecutionasyncresource"><code>executionAsyncResource</code></a> APIs as they have usability issues, safety risks,
3551cb0ef41Sopenharmony_ciand performance implications. Async context tracking use cases are better
3561cb0ef41Sopenharmony_ciserved by the stable <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a> API. If you have a use case for
3571cb0ef41Sopenharmony_ci<code>createHook</code>, <code>AsyncHook</code>, or <code>executionAsyncResource</code> beyond the context
3581cb0ef41Sopenharmony_citracking need solved by <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a> or diagnostics data currently
3591cb0ef41Sopenharmony_ciprovided by <a href="diagnostics_channel.html">Diagnostics Channel</a>, please open an issue at
3601cb0ef41Sopenharmony_ci<a href="https://github.com/nodejs/node/issues">https://github.com/nodejs/node/issues</a> describing your use case so we can
3611cb0ef41Sopenharmony_cicreate a more purpose-focused API.</div><p></p>
3621cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/async_hooks.js">lib/async_hooks.js</a></p>
3631cb0ef41Sopenharmony_ci<p>We strongly discourage the use of the <code>async_hooks</code> API.
3641cb0ef41Sopenharmony_ciOther APIs that can cover most of its use cases include:</p>
3651cb0ef41Sopenharmony_ci<ul>
3661cb0ef41Sopenharmony_ci<li><a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a> tracks async context</li>
3671cb0ef41Sopenharmony_ci<li><a href="process.html#processgetactiveresourcesinfo"><code>process.getActiveResourcesInfo()</code></a> tracks active resources</li>
3681cb0ef41Sopenharmony_ci</ul>
3691cb0ef41Sopenharmony_ci<p>The <code>node:async_hooks</code> module provides an API to track asynchronous resources.
3701cb0ef41Sopenharmony_ciIt can be accessed using:</p>
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ci<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>
3731cb0ef41Sopenharmony_ci<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>
3741cb0ef41Sopenharmony_ci<p>An asynchronous resource represents an object with an associated callback.
3751cb0ef41Sopenharmony_ciThis callback may be called multiple times, such as the <code>'connection'</code>
3761cb0ef41Sopenharmony_cievent in <code>net.createServer()</code>, or just a single time like in <code>fs.open()</code>.
3771cb0ef41Sopenharmony_ciA resource can also be closed before the callback is called. <code>AsyncHook</code> does
3781cb0ef41Sopenharmony_cinot explicitly distinguish between these different cases but will represent them
3791cb0ef41Sopenharmony_cias the abstract concept that is a resource.</p>
3801cb0ef41Sopenharmony_ci<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>
3811cb0ef41Sopenharmony_ciinterface, and each thread will use a new set of async IDs.</p>
3821cb0ef41Sopenharmony_ci</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>
3831cb0ef41Sopenharmony_ci<p>Following is a simple overview of the public API.</p>
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci<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>;
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Return the ID of the current execution context.</span>
3881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci<span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback of the</span>
3911cb0ef41Sopenharmony_ci<span class="hljs-comment">// current execution scope to call.</span>
3921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.</span>
3951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> asyncHook =
3961cb0ef41Sopenharmony_ci    async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, promiseResolve });
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_ci<span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an implicit</span>
3991cb0ef41Sopenharmony_ci<span class="hljs-comment">// action after running the constructor, and must be explicitly run to begin</span>
4001cb0ef41Sopenharmony_ci<span class="hljs-comment">// executing callbacks.</span>
4011cb0ef41Sopenharmony_ciasyncHook.<span class="hljs-title function_">enable</span>();
4021cb0ef41Sopenharmony_ci
4031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Disable listening for new asynchronous events.</span>
4041cb0ef41Sopenharmony_ciasyncHook.<span class="hljs-title function_">disable</span>();
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
4071cb0ef41Sopenharmony_ci<span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</span>
4081cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
4091cb0ef41Sopenharmony_ci
4101cb0ef41Sopenharmony_ci<span class="hljs-comment">// init() is called during object construction. The resource may not have</span>
4111cb0ef41Sopenharmony_ci<span class="hljs-comment">// completed construction when this callback runs. Therefore, all fields of the</span>
4121cb0ef41Sopenharmony_ci<span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
4131cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { }
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ci<span class="hljs-comment">// before() is called just before the resource's callback is called. It can be</span>
4161cb0ef41Sopenharmony_ci<span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called exactly 1</span>
4171cb0ef41Sopenharmony_ci<span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
4181cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
4191cb0ef41Sopenharmony_ci
4201cb0ef41Sopenharmony_ci<span class="hljs-comment">// after() is called just after the resource's callback has finished.</span>
4211cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci<span class="hljs-comment">// destroy() is called when the resource is destroyed.</span>
4241cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ci<span class="hljs-comment">// promiseResolve() is called only for promise resources, when the</span>
4271cb0ef41Sopenharmony_ci<span class="hljs-comment">// resolve() function passed to the Promise constructor is invoked</span>
4281cb0ef41Sopenharmony_ci<span class="hljs-comment">// (either directly or through other means of resolving a promise).</span>
4291cb0ef41Sopenharmony_ci<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>);
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Return the ID of the current execution context.</span>
4321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ci<span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback of the</span>
4351cb0ef41Sopenharmony_ci<span class="hljs-comment">// current execution scope to call.</span>
4361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.</span>
4391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> asyncHook =
4401cb0ef41Sopenharmony_ci    async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, promiseResolve });
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an implicit</span>
4431cb0ef41Sopenharmony_ci<span class="hljs-comment">// action after running the constructor, and must be explicitly run to begin</span>
4441cb0ef41Sopenharmony_ci<span class="hljs-comment">// executing callbacks.</span>
4451cb0ef41Sopenharmony_ciasyncHook.<span class="hljs-title function_">enable</span>();
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Disable listening for new asynchronous events.</span>
4481cb0ef41Sopenharmony_ciasyncHook.<span class="hljs-title function_">disable</span>();
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
4511cb0ef41Sopenharmony_ci<span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</span>
4521cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ci<span class="hljs-comment">// init() is called during object construction. The resource may not have</span>
4551cb0ef41Sopenharmony_ci<span class="hljs-comment">// completed construction when this callback runs. Therefore, all fields of the</span>
4561cb0ef41Sopenharmony_ci<span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
4571cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { }
4581cb0ef41Sopenharmony_ci
4591cb0ef41Sopenharmony_ci<span class="hljs-comment">// before() is called just before the resource's callback is called. It can be</span>
4601cb0ef41Sopenharmony_ci<span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called exactly 1</span>
4611cb0ef41Sopenharmony_ci<span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
4621cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci<span class="hljs-comment">// after() is called just after the resource's callback has finished.</span>
4651cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci<span class="hljs-comment">// destroy() is called when the resource is destroyed.</span>
4681cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci<span class="hljs-comment">// promiseResolve() is called only for promise resources, when the</span>
4711cb0ef41Sopenharmony_ci<span class="hljs-comment">// resolve() function passed to the Promise constructor is invoked</span>
4721cb0ef41Sopenharmony_ci<span class="hljs-comment">// (either directly or through other means of resolving a promise).</span>
4731cb0ef41Sopenharmony_ci<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>
4741cb0ef41Sopenharmony_ci</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>
4751cb0ef41Sopenharmony_ci<div class="api_metadata">
4761cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span>
4771cb0ef41Sopenharmony_ci</div>
4781cb0ef41Sopenharmony_ci<ul>
4791cb0ef41Sopenharmony_ci<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
4801cb0ef41Sopenharmony_ci<ul>
4811cb0ef41Sopenharmony_ci<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>
4821cb0ef41Sopenharmony_ci<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>
4831cb0ef41Sopenharmony_ci<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>
4841cb0ef41Sopenharmony_ci<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>
4851cb0ef41Sopenharmony_ci<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>
4861cb0ef41Sopenharmony_ci</ul>
4871cb0ef41Sopenharmony_ci</li>
4881cb0ef41Sopenharmony_ci<li>Returns: <a href="async_hooks.html#async_hookscreatehookcallbacks" class="type">&#x3C;AsyncHook></a> Instance used for disabling and enabling hooks</li>
4891cb0ef41Sopenharmony_ci</ul>
4901cb0ef41Sopenharmony_ci<p>Registers functions to be called for different lifetime events of each async
4911cb0ef41Sopenharmony_cioperation.</p>
4921cb0ef41Sopenharmony_ci<p>The callbacks <code>init()</code>/<code>before()</code>/<code>after()</code>/<code>destroy()</code> are called for the
4931cb0ef41Sopenharmony_cirespective asynchronous event during a resource's lifetime.</p>
4941cb0ef41Sopenharmony_ci<p>All callbacks are optional. For example, if only resource cleanup needs to
4951cb0ef41Sopenharmony_cibe tracked, then only the <code>destroy</code> callback needs to be passed. The
4961cb0ef41Sopenharmony_cispecifics of all functions that can be passed to <code>callbacks</code> is in the
4971cb0ef41Sopenharmony_ci<a href="#hook-callbacks">Hook Callbacks</a> section.</p>
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci<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>;
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> asyncHook = <span class="hljs-title function_">createHook</span>({
5021cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { },
5031cb0ef41Sopenharmony_ci  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { },
5041cb0ef41Sopenharmony_ci});</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>);
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">createHook</span>({
5071cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { },
5081cb0ef41Sopenharmony_ci  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { },
5091cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
5101cb0ef41Sopenharmony_ci<p>The callbacks will be inherited via the prototype chain:</p>
5111cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyAsyncCallbacks</span> {
5121cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) { }
5131cb0ef41Sopenharmony_ci  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {}
5141cb0ef41Sopenharmony_ci}
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci<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> {
5171cb0ef41Sopenharmony_ci  <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
5181cb0ef41Sopenharmony_ci  <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
5191cb0ef41Sopenharmony_ci}
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci<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>
5221cb0ef41Sopenharmony_ci<p>Because promises are asynchronous resources whose lifecycle is tracked
5231cb0ef41Sopenharmony_civia the async hooks mechanism, the <code>init()</code>, <code>before()</code>, <code>after()</code>, and
5241cb0ef41Sopenharmony_ci<code>destroy()</code> callbacks <em>must not</em> be async functions that return promises.</p>
5251cb0ef41Sopenharmony_ci<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>
5261cb0ef41Sopenharmony_ci<p>If any <code>AsyncHook</code> callbacks throw, the application will print the stack trace
5271cb0ef41Sopenharmony_ciand exit. The exit path does follow that of an uncaught exception, but
5281cb0ef41Sopenharmony_ciall <code>'uncaughtException'</code> listeners are removed, thus forcing the process to
5291cb0ef41Sopenharmony_ciexit. The <code>'exit'</code> callbacks will still be called unless the application is run
5301cb0ef41Sopenharmony_ciwith <code>--abort-on-uncaught-exception</code>, in which case a stack trace will be
5311cb0ef41Sopenharmony_ciprinted and the application exits, leaving a core file.</p>
5321cb0ef41Sopenharmony_ci<p>The reason for this error handling behavior is that these callbacks are running
5331cb0ef41Sopenharmony_ciat potentially volatile points in an object's lifetime, for example during
5341cb0ef41Sopenharmony_ciclass construction and destruction. Because of this, it is deemed necessary to
5351cb0ef41Sopenharmony_cibring down the process quickly in order to prevent an unintentional abort in the
5361cb0ef41Sopenharmony_cifuture. This is subject to change in the future if a comprehensive analysis is
5371cb0ef41Sopenharmony_ciperformed to ensure an exception can follow the normal control flow without
5381cb0ef41Sopenharmony_ciunintentional side effects.</p>
5391cb0ef41Sopenharmony_ci<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>
5401cb0ef41Sopenharmony_ci<p>Because printing to the console is an asynchronous operation, <code>console.log()</code>
5411cb0ef41Sopenharmony_ciwill cause <code>AsyncHook</code> callbacks to be called. Using <code>console.log()</code> or
5421cb0ef41Sopenharmony_cisimilar asynchronous operations inside an <code>AsyncHook</code> callback function will
5431cb0ef41Sopenharmony_cicause an infinite recursion. An easy solution to this when debugging is to use a
5441cb0ef41Sopenharmony_cisynchronous logging operation such as <code>fs.writeFileSync(file, msg, flag)</code>.
5451cb0ef41Sopenharmony_ciThis will print to the file and will not invoke <code>AsyncHook</code> recursively because
5461cb0ef41Sopenharmony_ciit is synchronous.</p>
5471cb0ef41Sopenharmony_ci
5481cb0ef41Sopenharmony_ci<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>;
5491cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { format } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>;
5501cb0ef41Sopenharmony_ci
5511cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span class="hljs-params">...args</span>) {
5521cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHook callback</span>
5531cb0ef41Sopenharmony_ci  <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> });
5541cb0ef41Sopenharmony_ci}</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>);
5551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>);
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span class="hljs-params">...args</span>) {
5581cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHook callback</span>
5591cb0ef41Sopenharmony_ci  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> });
5601cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre>
5611cb0ef41Sopenharmony_ci<p>If an asynchronous operation is needed for logging, it is possible to keep
5621cb0ef41Sopenharmony_citrack of what caused the asynchronous operation using the information
5631cb0ef41Sopenharmony_ciprovided by <code>AsyncHook</code> itself. The logging should then be skipped when
5641cb0ef41Sopenharmony_ciit was the logging itself that caused the <code>AsyncHook</code> callback to be called. By
5651cb0ef41Sopenharmony_cidoing this, the otherwise infinite recursion is broken.</p>
5661cb0ef41Sopenharmony_ci</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>
5671cb0ef41Sopenharmony_ci<p>The class <code>AsyncHook</code> exposes an interface for tracking lifetime events
5681cb0ef41Sopenharmony_ciof asynchronous operations.</p>
5691cb0ef41Sopenharmony_ci<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>
5701cb0ef41Sopenharmony_ci<ul>
5711cb0ef41Sopenharmony_ci<li>Returns: <a href="async_hooks.html#async_hookscreatehookcallbacks" class="type">&#x3C;AsyncHook></a> A reference to <code>asyncHook</code>.</li>
5721cb0ef41Sopenharmony_ci</ul>
5731cb0ef41Sopenharmony_ci<p>Enable the callbacks for a given <code>AsyncHook</code> instance. If no callbacks are
5741cb0ef41Sopenharmony_ciprovided, enabling is a no-op.</p>
5751cb0ef41Sopenharmony_ci<p>The <code>AsyncHook</code> instance is disabled by default. If the <code>AsyncHook</code> instance
5761cb0ef41Sopenharmony_cishould be enabled immediately after creation, the following pattern can be used.</p>
5771cb0ef41Sopenharmony_ci
5781cb0ef41Sopenharmony_ci<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>;
5791cb0ef41Sopenharmony_ci
5801cb0ef41Sopenharmony_ci<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>);
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_ci<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>
5831cb0ef41Sopenharmony_ci<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>
5841cb0ef41Sopenharmony_ci<ul>
5851cb0ef41Sopenharmony_ci<li>Returns: <a href="async_hooks.html#async_hookscreatehookcallbacks" class="type">&#x3C;AsyncHook></a> A reference to <code>asyncHook</code>.</li>
5861cb0ef41Sopenharmony_ci</ul>
5871cb0ef41Sopenharmony_ci<p>Disable the callbacks for a given <code>AsyncHook</code> instance from the global pool of
5881cb0ef41Sopenharmony_ci<code>AsyncHook</code> callbacks to be executed. Once a hook has been disabled it will not
5891cb0ef41Sopenharmony_cibe called again until enabled.</p>
5901cb0ef41Sopenharmony_ci<p>For API consistency <code>disable()</code> also returns the <code>AsyncHook</code> instance.</p>
5911cb0ef41Sopenharmony_ci<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>
5921cb0ef41Sopenharmony_ci<p>Key events in the lifetime of asynchronous events have been categorized into
5931cb0ef41Sopenharmony_cifour areas: instantiation, before/after the callback is called, and when the
5941cb0ef41Sopenharmony_ciinstance is destroyed.</p>
5951cb0ef41Sopenharmony_ci<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>
5961cb0ef41Sopenharmony_ci<ul>
5971cb0ef41Sopenharmony_ci<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>
5981cb0ef41Sopenharmony_ci<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>
5991cb0ef41Sopenharmony_ci<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
6001cb0ef41Sopenharmony_ciexecution context this async resource was created.</li>
6011cb0ef41Sopenharmony_ci<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
6021cb0ef41Sopenharmony_cioperation, needs to be released during <em>destroy</em>.</li>
6031cb0ef41Sopenharmony_ci</ul>
6041cb0ef41Sopenharmony_ci<p>Called when a class is constructed that has the <em>possibility</em> to emit an
6051cb0ef41Sopenharmony_ciasynchronous event. This <em>does not</em> mean the instance must call
6061cb0ef41Sopenharmony_ci<code>before</code>/<code>after</code> before <code>destroy</code> is called, only that the possibility
6071cb0ef41Sopenharmony_ciexists.</p>
6081cb0ef41Sopenharmony_ci<p>This behavior can be observed by doing something like opening a resource then
6091cb0ef41Sopenharmony_ciclosing it before the resource can be used. The following snippet demonstrates
6101cb0ef41Sopenharmony_cithis.</p>
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ci<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>;
6131cb0ef41Sopenharmony_ci
6141cb0ef41Sopenharmony_ci<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>(); });
6151cb0ef41Sopenharmony_ci<span class="hljs-comment">// OR</span>
6161cb0ef41Sopenharmony_ci<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>(); });
6171cb0ef41Sopenharmony_ci<span class="hljs-comment">// OR</span>
6181cb0ef41Sopenharmony_ci<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>
6191cb0ef41Sopenharmony_ci<p>Every new resource is assigned an ID that is unique within the scope of the
6201cb0ef41Sopenharmony_cicurrent Node.js instance.</p>
6211cb0ef41Sopenharmony_ci<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>
6221cb0ef41Sopenharmony_ci<p>The <code>type</code> is a string identifying the type of resource that caused
6231cb0ef41Sopenharmony_ci<code>init</code> to be called. Generally, it will correspond to the name of the
6241cb0ef41Sopenharmony_ciresource's constructor.</p>
6251cb0ef41Sopenharmony_ci<p>The <code>type</code> of resources created by Node.js itself can change in any Node.js
6261cb0ef41Sopenharmony_cirelease. Valid values include <code>TLSWRAP</code>,
6271cb0ef41Sopenharmony_ci<code>TCPWRAP</code>, <code>TCPSERVERWRAP</code>, <code>GETADDRINFOREQWRAP</code>, <code>FSREQCALLBACK</code>,
6281cb0ef41Sopenharmony_ci<code>Microtask</code>, and <code>Timeout</code>. Inspect the source code of the Node.js version used
6291cb0ef41Sopenharmony_cito get the full list.</p>
6301cb0ef41Sopenharmony_ci<p>Furthermore users of <a href="async_context.html#class-asyncresource"><code>AsyncResource</code></a> create async resources independent
6311cb0ef41Sopenharmony_ciof Node.js itself.</p>
6321cb0ef41Sopenharmony_ci<p>There is also the <code>PROMISE</code> resource type, which is used to track <code>Promise</code>
6331cb0ef41Sopenharmony_ciinstances and asynchronous work scheduled by them.</p>
6341cb0ef41Sopenharmony_ci<p>Users are able to define their own <code>type</code> when using the public embedder API.</p>
6351cb0ef41Sopenharmony_ci<p>It is possible to have type name collisions. Embedders are encouraged to use
6361cb0ef41Sopenharmony_ciunique prefixes, such as the npm package name, to prevent collisions when
6371cb0ef41Sopenharmony_cilistening to the hooks.</p>
6381cb0ef41Sopenharmony_ci<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>
6391cb0ef41Sopenharmony_ci<p><code>triggerAsyncId</code> is the <code>asyncId</code> of the resource that caused (or "triggered")
6401cb0ef41Sopenharmony_cithe new resource to initialize and that caused <code>init</code> to call. This is different
6411cb0ef41Sopenharmony_cifrom <code>async_hooks.executionAsyncId()</code> that only shows <em>when</em> a resource was
6421cb0ef41Sopenharmony_cicreated, while <code>triggerAsyncId</code> shows <em>why</em> a resource was created.</p>
6431cb0ef41Sopenharmony_ci<p>The following is a simple demonstration of <code>triggerAsyncId</code>:</p>
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ci<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>;
6461cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
6471cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> net <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
6481cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_ci<span class="hljs-title function_">createHook</span>({
6511cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
6521cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> eid = <span class="hljs-title function_">executionAsyncId</span>();
6531cb0ef41Sopenharmony_ci    fs.<span class="hljs-title function_">writeSync</span>(
6541cb0ef41Sopenharmony_ci      stdout.<span class="hljs-property">fd</span>,
6551cb0ef41Sopenharmony_ci      <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>);
6561cb0ef41Sopenharmony_ci  },
6571cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">enable</span>();
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_cinet.<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>);
6601cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { stdout } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>);
6611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
6621cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
6631cb0ef41Sopenharmony_ci
6641cb0ef41Sopenharmony_ci<span class="hljs-title function_">createHook</span>({
6651cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
6661cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> eid = <span class="hljs-title function_">executionAsyncId</span>();
6671cb0ef41Sopenharmony_ci    fs.<span class="hljs-title function_">writeSync</span>(
6681cb0ef41Sopenharmony_ci      stdout.<span class="hljs-property">fd</span>,
6691cb0ef41Sopenharmony_ci      <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>);
6701cb0ef41Sopenharmony_ci  },
6711cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">enable</span>();
6721cb0ef41Sopenharmony_ci
6731cb0ef41Sopenharmony_cinet.<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>
6741cb0ef41Sopenharmony_ci<p>Output when hitting the server with <code>nc localhost 8080</code>:</p>
6751cb0ef41Sopenharmony_ci<pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6761cb0ef41Sopenharmony_ciTCPWRAP(7): trigger: 5 execution: 0</code> <button class="copy-button">copy</button></pre>
6771cb0ef41Sopenharmony_ci<p>The <code>TCPSERVERWRAP</code> is the server which receives the connections.</p>
6781cb0ef41Sopenharmony_ci<p>The <code>TCPWRAP</code> is the new connection from the client. When a new
6791cb0ef41Sopenharmony_ciconnection is made, the <code>TCPWrap</code> instance is immediately constructed. This
6801cb0ef41Sopenharmony_cihappens outside of any JavaScript stack. (An <code>executionAsyncId()</code> of <code>0</code> means
6811cb0ef41Sopenharmony_cithat it is being executed from C++ with no JavaScript stack above it.) With only
6821cb0ef41Sopenharmony_cithat information, it would be impossible to link resources together in
6831cb0ef41Sopenharmony_citerms of what caused them to be created, so <code>triggerAsyncId</code> is given the task
6841cb0ef41Sopenharmony_ciof propagating what resource is responsible for the new resource's existence.</p>
6851cb0ef41Sopenharmony_ci<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>
6861cb0ef41Sopenharmony_ci<p><code>resource</code> is an object that represents the actual async resource that has
6871cb0ef41Sopenharmony_cibeen initialized. The API to access the object may be specified by the
6881cb0ef41Sopenharmony_cicreator of the resource. Resources created by Node.js itself are internal
6891cb0ef41Sopenharmony_ciand may change at any time. Therefore no API is specified for these.</p>
6901cb0ef41Sopenharmony_ci<p>In some cases the resource object is reused for performance reasons, it is
6911cb0ef41Sopenharmony_cithus not safe to use it as a key in a <code>WeakMap</code> or add properties to it.</p>
6921cb0ef41Sopenharmony_ci<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>
6931cb0ef41Sopenharmony_ci<p>The context tracking use case is covered by the stable API <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.
6941cb0ef41Sopenharmony_ciThis example only illustrates async hooks operation but <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>
6951cb0ef41Sopenharmony_cifits better to this use case.</p>
6961cb0ef41Sopenharmony_ci<p>The following is an example with additional information about the calls to
6971cb0ef41Sopenharmony_ci<code>init</code> between the <code>before</code> and <code>after</code> calls, specifically what the
6981cb0ef41Sopenharmony_cicallback to <code>listen()</code> will look like. The output formatting is slightly more
6991cb0ef41Sopenharmony_cielaborate to make calling context easier to see.</p>
7001cb0ef41Sopenharmony_ci
7011cb0ef41Sopenharmony_ci<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>;
7021cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
7031cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> net <span class="hljs-keyword">from</span> <span class="hljs-string">'node:net'</span>;
7041cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
7051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { fd } = stdout;
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
7081cb0ef41Sopenharmony_ciasync_hooks.<span class="hljs-title function_">createHook</span>({
7091cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
7101cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
7111cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7121cb0ef41Sopenharmony_ci    fs.<span class="hljs-title function_">writeSync</span>(
7131cb0ef41Sopenharmony_ci      fd,
7141cb0ef41Sopenharmony_ci      <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>):`</span> +
7151cb0ef41Sopenharmony_ci      <span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: <span class="hljs-subst">${eid}</span>\n`</span>);
7161cb0ef41Sopenharmony_ci  },
7171cb0ef41Sopenharmony_ci  <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
7181cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7191cb0ef41Sopenharmony_ci    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>);
7201cb0ef41Sopenharmony_ci    indent += <span class="hljs-number">2</span>;
7211cb0ef41Sopenharmony_ci  },
7221cb0ef41Sopenharmony_ci  <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
7231cb0ef41Sopenharmony_ci    indent -= <span class="hljs-number">2</span>;
7241cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7251cb0ef41Sopenharmony_ci    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>);
7261cb0ef41Sopenharmony_ci  },
7271cb0ef41Sopenharmony_ci  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
7281cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7291cb0ef41Sopenharmony_ci    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>);
7301cb0ef41Sopenharmony_ci  },
7311cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">enable</span>();
7321cb0ef41Sopenharmony_ci
7331cb0ef41Sopenharmony_cinet.<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> {
7341cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
7351cb0ef41Sopenharmony_ci  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
7361cb0ef41Sopenharmony_ci    <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>());
7371cb0ef41Sopenharmony_ci  }, <span class="hljs-number">10</span>);
7381cb0ef41Sopenharmony_ci});</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>);
7391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
7401cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
7411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { fd } = process.<span class="hljs-property">stdout</span>;
7421cb0ef41Sopenharmony_ci
7431cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
7441cb0ef41Sopenharmony_ciasync_hooks.<span class="hljs-title function_">createHook</span>({
7451cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId</span>) {
7461cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
7471cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7481cb0ef41Sopenharmony_ci    fs.<span class="hljs-title function_">writeSync</span>(
7491cb0ef41Sopenharmony_ci      fd,
7501cb0ef41Sopenharmony_ci      <span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>):`</span> +
7511cb0ef41Sopenharmony_ci      <span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: <span class="hljs-subst">${eid}</span>\n`</span>);
7521cb0ef41Sopenharmony_ci  },
7531cb0ef41Sopenharmony_ci  <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
7541cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7551cb0ef41Sopenharmony_ci    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>);
7561cb0ef41Sopenharmony_ci    indent += <span class="hljs-number">2</span>;
7571cb0ef41Sopenharmony_ci  },
7581cb0ef41Sopenharmony_ci  <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
7591cb0ef41Sopenharmony_ci    indent -= <span class="hljs-number">2</span>;
7601cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7611cb0ef41Sopenharmony_ci    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>);
7621cb0ef41Sopenharmony_ci  },
7631cb0ef41Sopenharmony_ci  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
7641cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(indent);
7651cb0ef41Sopenharmony_ci    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>);
7661cb0ef41Sopenharmony_ci  },
7671cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">enable</span>();
7681cb0ef41Sopenharmony_ci
7691cb0ef41Sopenharmony_cinet.<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> {
7701cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
7711cb0ef41Sopenharmony_ci  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
7721cb0ef41Sopenharmony_ci    <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>());
7731cb0ef41Sopenharmony_ci  }, <span class="hljs-number">10</span>);
7741cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
7751cb0ef41Sopenharmony_ci<p>Output from only starting the server:</p>
7761cb0ef41Sopenharmony_ci<pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
7771cb0ef41Sopenharmony_ciTickObject(6): trigger: 5 execution: 1
7781cb0ef41Sopenharmony_cibefore:  6
7791cb0ef41Sopenharmony_ci  Timeout(7): trigger: 6 execution: 6
7801cb0ef41Sopenharmony_ciafter:   6
7811cb0ef41Sopenharmony_cidestroy: 6
7821cb0ef41Sopenharmony_cibefore:  7
7831cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">></span><span class="language-bash">>> 7</span>
7841cb0ef41Sopenharmony_ci  TickObject(8): trigger: 7 execution: 7
7851cb0ef41Sopenharmony_ciafter:   7
7861cb0ef41Sopenharmony_cibefore:  8
7871cb0ef41Sopenharmony_ciafter:   8</code> <button class="copy-button">copy</button></pre>
7881cb0ef41Sopenharmony_ci<p>As illustrated in the example, <code>executionAsyncId()</code> and <code>execution</code> each specify
7891cb0ef41Sopenharmony_cithe value of the current execution context; which is delineated by calls to
7901cb0ef41Sopenharmony_ci<code>before</code> and <code>after</code>.</p>
7911cb0ef41Sopenharmony_ci<p>Only using <code>execution</code> to graph resource allocation results in the following:</p>
7921cb0ef41Sopenharmony_ci<pre><code class="language-console">  root(1)
7931cb0ef41Sopenharmony_ci     ^
7941cb0ef41Sopenharmony_ci     |
7951cb0ef41Sopenharmony_ciTickObject(6)
7961cb0ef41Sopenharmony_ci     ^
7971cb0ef41Sopenharmony_ci     |
7981cb0ef41Sopenharmony_ci Timeout(7)</code> <button class="copy-button">copy</button></pre>
7991cb0ef41Sopenharmony_ci<p>The <code>TCPSERVERWRAP</code> is not part of this graph, even though it was the reason for
8001cb0ef41Sopenharmony_ci<code>console.log()</code> being called. This is because binding to a port without a host
8011cb0ef41Sopenharmony_ciname is a <em>synchronous</em> operation, but to maintain a completely asynchronous
8021cb0ef41Sopenharmony_ciAPI the user's callback is placed in a <code>process.nextTick()</code>. Which is why
8031cb0ef41Sopenharmony_ci<code>TickObject</code> is present in the output and is a 'parent' for <code>.listen()</code>
8041cb0ef41Sopenharmony_cicallback.</p>
8051cb0ef41Sopenharmony_ci<p>The graph only shows <em>when</em> a resource was created, not <em>why</em>, so to track
8061cb0ef41Sopenharmony_cithe <em>why</em> use <code>triggerAsyncId</code>. Which can be represented with the following
8071cb0ef41Sopenharmony_cigraph:</p>
8081cb0ef41Sopenharmony_ci<pre><code class="language-console"> bootstrap(1)
8091cb0ef41Sopenharmony_ci     |
8101cb0ef41Sopenharmony_ci     ˅
8111cb0ef41Sopenharmony_ciTCPSERVERWRAP(5)
8121cb0ef41Sopenharmony_ci     |
8131cb0ef41Sopenharmony_ci     ˅
8141cb0ef41Sopenharmony_ci TickObject(6)
8151cb0ef41Sopenharmony_ci     |
8161cb0ef41Sopenharmony_ci     ˅
8171cb0ef41Sopenharmony_ci  Timeout(7)</code> <button class="copy-button">copy</button></pre>
8181cb0ef41Sopenharmony_ci<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>
8191cb0ef41Sopenharmony_ci<ul>
8201cb0ef41Sopenharmony_ci<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>
8211cb0ef41Sopenharmony_ci</ul>
8221cb0ef41Sopenharmony_ci<p>When an asynchronous operation is initiated (such as a TCP server receiving a
8231cb0ef41Sopenharmony_cinew connection) or completes (such as writing data to disk) a callback is
8241cb0ef41Sopenharmony_cicalled to notify the user. The <code>before</code> callback is called just before said
8251cb0ef41Sopenharmony_cicallback is executed. <code>asyncId</code> is the unique identifier assigned to the
8261cb0ef41Sopenharmony_ciresource about to execute the callback.</p>
8271cb0ef41Sopenharmony_ci<p>The <code>before</code> callback will be called 0 to N times. The <code>before</code> callback
8281cb0ef41Sopenharmony_ciwill typically be called 0 times if the asynchronous operation was cancelled
8291cb0ef41Sopenharmony_cior, for example, if no connections are received by a TCP server. Persistent
8301cb0ef41Sopenharmony_ciasynchronous resources like a TCP server will typically call the <code>before</code>
8311cb0ef41Sopenharmony_cicallback multiple times, while other operations like <code>fs.open()</code> will call
8321cb0ef41Sopenharmony_ciit only once.</p>
8331cb0ef41Sopenharmony_ci<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>
8341cb0ef41Sopenharmony_ci<ul>
8351cb0ef41Sopenharmony_ci<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>
8361cb0ef41Sopenharmony_ci</ul>
8371cb0ef41Sopenharmony_ci<p>Called immediately after the callback specified in <code>before</code> is completed.</p>
8381cb0ef41Sopenharmony_ci<p>If an uncaught exception occurs during execution of the callback, then <code>after</code>
8391cb0ef41Sopenharmony_ciwill run <em>after</em> the <code>'uncaughtException'</code> event is emitted or a <code>domain</code>'s
8401cb0ef41Sopenharmony_cihandler runs.</p>
8411cb0ef41Sopenharmony_ci<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>
8421cb0ef41Sopenharmony_ci<ul>
8431cb0ef41Sopenharmony_ci<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>
8441cb0ef41Sopenharmony_ci</ul>
8451cb0ef41Sopenharmony_ci<p>Called after the resource corresponding to <code>asyncId</code> is destroyed. It is also
8461cb0ef41Sopenharmony_cicalled asynchronously from the embedder API <code>emitDestroy()</code>.</p>
8471cb0ef41Sopenharmony_ci<p>Some resources depend on garbage collection for cleanup, so if a reference is
8481cb0ef41Sopenharmony_cimade to the <code>resource</code> object passed to <code>init</code> it is possible that <code>destroy</code>
8491cb0ef41Sopenharmony_ciwill never be called, causing a memory leak in the application. If the resource
8501cb0ef41Sopenharmony_cidoes not depend on garbage collection, then this will not be an issue.</p>
8511cb0ef41Sopenharmony_ci<p>Using the destroy hook results in additional overhead because it enables
8521cb0ef41Sopenharmony_citracking of <code>Promise</code> instances via the garbage collector.</p>
8531cb0ef41Sopenharmony_ci<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>
8541cb0ef41Sopenharmony_ci<div class="api_metadata">
8551cb0ef41Sopenharmony_ci<span>Added in: v8.6.0</span>
8561cb0ef41Sopenharmony_ci</div>
8571cb0ef41Sopenharmony_ci<ul>
8581cb0ef41Sopenharmony_ci<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>
8591cb0ef41Sopenharmony_ci</ul>
8601cb0ef41Sopenharmony_ci<p>Called when the <code>resolve</code> function passed to the <code>Promise</code> constructor is
8611cb0ef41Sopenharmony_ciinvoked (either directly or through other means of resolving a promise).</p>
8621cb0ef41Sopenharmony_ci<p><code>resolve()</code> does not do any observable synchronous work.</p>
8631cb0ef41Sopenharmony_ci<p>The <code>Promise</code> is not necessarily fulfilled or rejected at this point if the
8641cb0ef41Sopenharmony_ci<code>Promise</code> was resolved by assuming the state of another <code>Promise</code>.</p>
8651cb0ef41Sopenharmony_ci<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>
8661cb0ef41Sopenharmony_ci<p>calls the following callbacks:</p>
8671cb0ef41Sopenharmony_ci<pre><code class="language-text">init for PROMISE with id 5, trigger id: 1
8681cb0ef41Sopenharmony_ci  promise resolve 5      # corresponds to resolve(true)
8691cb0ef41Sopenharmony_ciinit for PROMISE with id 6, trigger id: 5  # the Promise returned by then()
8701cb0ef41Sopenharmony_ci  before 6               # the then() callback is entered
8711cb0ef41Sopenharmony_ci  promise resolve 6      # the then() callback resolves the promise by returning
8721cb0ef41Sopenharmony_ci  after 6</code> <button class="copy-button">copy</button></pre>
8731cb0ef41Sopenharmony_ci<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>
8741cb0ef41Sopenharmony_ci<div class="api_metadata">
8751cb0ef41Sopenharmony_ci<span>Added in: v13.9.0, v12.17.0</span>
8761cb0ef41Sopenharmony_ci</div>
8771cb0ef41Sopenharmony_ci<ul>
8781cb0ef41Sopenharmony_ci<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.
8791cb0ef41Sopenharmony_ciUseful to store data within the resource.</li>
8801cb0ef41Sopenharmony_ci</ul>
8811cb0ef41Sopenharmony_ci<p>Resource objects returned by <code>executionAsyncResource()</code> are most often internal
8821cb0ef41Sopenharmony_ciNode.js handle objects with undocumented APIs. Using any functions or properties
8831cb0ef41Sopenharmony_cion the object is likely to crash your application and should be avoided.</p>
8841cb0ef41Sopenharmony_ci<p>Using <code>executionAsyncResource()</code> in the top-level execution context will
8851cb0ef41Sopenharmony_cireturn an empty object as there is no handle or request object to use,
8861cb0ef41Sopenharmony_cibut having an object representing the top-level can be helpful.</p>
8871cb0ef41Sopenharmony_ci
8881cb0ef41Sopenharmony_ci<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>;
8891cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { executionAsyncId, executionAsyncResource } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>;
8901cb0ef41Sopenharmony_ci
8911cb0ef41Sopenharmony_ci<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>
8921cb0ef41Sopenharmony_ci<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> {
8931cb0ef41Sopenharmony_ci  <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>
8941cb0ef41Sopenharmony_ci});</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>);
8951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { executionAsyncId, executionAsyncResource } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
8961cb0ef41Sopenharmony_ci
8971cb0ef41Sopenharmony_ci<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>
8981cb0ef41Sopenharmony_ci<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> {
8991cb0ef41Sopenharmony_ci  <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>
9001cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
9011cb0ef41Sopenharmony_ci<p>This can be used to implement continuation local storage without the
9021cb0ef41Sopenharmony_ciuse of a tracking <code>Map</code> to store the metadata:</p>
9031cb0ef41Sopenharmony_ci
9041cb0ef41Sopenharmony_ci<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>;
9051cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> {
9061cb0ef41Sopenharmony_ci  executionAsyncId,
9071cb0ef41Sopenharmony_ci  executionAsyncResource,
9081cb0ef41Sopenharmony_ci  createHook,
9091cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'async_hooks'</span>;
9101cb0ef41Sopenharmony_ci<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>
9111cb0ef41Sopenharmony_ci
9121cb0ef41Sopenharmony_ci<span class="hljs-title function_">createHook</span>({
9131cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) {
9141cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncResource</span>();
9151cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (cr) {
9161cb0ef41Sopenharmony_ci      resource[sym] = cr[sym];
9171cb0ef41Sopenharmony_ci    }
9181cb0ef41Sopenharmony_ci  },
9191cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">enable</span>();
9201cb0ef41Sopenharmony_ci
9211cb0ef41Sopenharmony_ci<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> {
9221cb0ef41Sopenharmony_ci  <span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr">state</span>: req.<span class="hljs-property">url</span> };
9231cb0ef41Sopenharmony_ci  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {
9241cb0ef41Sopenharmony_ci    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]));
9251cb0ef41Sopenharmony_ci  }, <span class="hljs-number">100</span>);
9261cb0ef41Sopenharmony_ci}).<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>);
9271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> {
9281cb0ef41Sopenharmony_ci  executionAsyncId,
9291cb0ef41Sopenharmony_ci  executionAsyncResource,
9301cb0ef41Sopenharmony_ci  createHook,
9311cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>);
9321cb0ef41Sopenharmony_ci<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>
9331cb0ef41Sopenharmony_ci
9341cb0ef41Sopenharmony_ci<span class="hljs-title function_">createHook</span>({
9351cb0ef41Sopenharmony_ci  <span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsyncId, resource</span>) {
9361cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncResource</span>();
9371cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (cr) {
9381cb0ef41Sopenharmony_ci      resource[sym] = cr[sym];
9391cb0ef41Sopenharmony_ci    }
9401cb0ef41Sopenharmony_ci  },
9411cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">enable</span>();
9421cb0ef41Sopenharmony_ci
9431cb0ef41Sopenharmony_ci<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> {
9441cb0ef41Sopenharmony_ci  <span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr">state</span>: req.<span class="hljs-property">url</span> };
9451cb0ef41Sopenharmony_ci  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {
9461cb0ef41Sopenharmony_ci    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]));
9471cb0ef41Sopenharmony_ci  }, <span class="hljs-number">100</span>);
9481cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><button class="copy-button">copy</button></pre>
9491cb0ef41Sopenharmony_ci<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>
9501cb0ef41Sopenharmony_ci<div class="api_metadata">
9511cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
9521cb0ef41Sopenharmony_ci<table>
9531cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
9541cb0ef41Sopenharmony_ci<tr><td>v8.2.0</td>
9551cb0ef41Sopenharmony_ci<td><p>Renamed from <code>currentId</code>.</p></td></tr>
9561cb0ef41Sopenharmony_ci<tr><td>v8.1.0</td>
9571cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.1.0</span></p></td></tr>
9581cb0ef41Sopenharmony_ci</tbody></table>
9591cb0ef41Sopenharmony_ci</details>
9601cb0ef41Sopenharmony_ci</div>
9611cb0ef41Sopenharmony_ci<ul>
9621cb0ef41Sopenharmony_ci<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
9631cb0ef41Sopenharmony_citrack when something calls.</li>
9641cb0ef41Sopenharmony_ci</ul>
9651cb0ef41Sopenharmony_ci
9661cb0ef41Sopenharmony_ci<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>;
9671cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
9681cb0ef41Sopenharmony_ci
9691cb0ef41Sopenharmony_ci<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>
9701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> path = <span class="hljs-string">'.'</span>;
9711cb0ef41Sopenharmony_cifs.<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> {
9721cb0ef41Sopenharmony_ci  <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>
9731cb0ef41Sopenharmony_ci});</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>);
9741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
9751cb0ef41Sopenharmony_ci
9761cb0ef41Sopenharmony_ci<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>
9771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> path = <span class="hljs-string">'.'</span>;
9781cb0ef41Sopenharmony_cifs.<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> {
9791cb0ef41Sopenharmony_ci  <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>
9801cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
9811cb0ef41Sopenharmony_ci<p>The ID returned from <code>executionAsyncId()</code> is related to execution timing, not
9821cb0ef41Sopenharmony_cicausality (which is covered by <code>triggerAsyncId()</code>):</p>
9831cb0ef41Sopenharmony_ci<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> {
9841cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Returns the ID of the server, not of the new connection, because the</span>
9851cb0ef41Sopenharmony_ci  <span class="hljs-comment">// callback runs in the execution scope of the server's MakeCallback().</span>
9861cb0ef41Sopenharmony_ci  async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
9871cb0ef41Sopenharmony_ci
9881cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
9891cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Returns the ID of a TickObject (process.nextTick()) because all</span>
9901cb0ef41Sopenharmony_ci  <span class="hljs-comment">// callbacks passed to .listen() are wrapped in a nextTick().</span>
9911cb0ef41Sopenharmony_ci  async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
9921cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9931cb0ef41Sopenharmony_ci<p>Promise contexts may not get precise <code>executionAsyncIds</code> by default.
9941cb0ef41Sopenharmony_ciSee the section on <a href="#promise-execution-tracking">promise execution tracking</a>.</p>
9951cb0ef41Sopenharmony_ci<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>
9961cb0ef41Sopenharmony_ci<ul>
9971cb0ef41Sopenharmony_ci<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
9981cb0ef41Sopenharmony_cithat is currently being executed.</li>
9991cb0ef41Sopenharmony_ci</ul>
10001cb0ef41Sopenharmony_ci<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> {
10011cb0ef41Sopenharmony_ci  <span class="hljs-comment">// The resource that caused (or triggered) this callback to be called</span>
10021cb0ef41Sopenharmony_ci  <span class="hljs-comment">// was that of the new connection. Thus the return value of triggerAsyncId()</span>
10031cb0ef41Sopenharmony_ci  <span class="hljs-comment">// is the asyncId of "conn".</span>
10041cb0ef41Sopenharmony_ci  async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
10051cb0ef41Sopenharmony_ci
10061cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
10071cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Even though all callbacks passed to .listen() are wrapped in a nextTick()</span>
10081cb0ef41Sopenharmony_ci  <span class="hljs-comment">// the callback itself exists because the call to the server's .listen()</span>
10091cb0ef41Sopenharmony_ci  <span class="hljs-comment">// was made. So the return value would be the ID of the server.</span>
10101cb0ef41Sopenharmony_ci  async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
10111cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
10121cb0ef41Sopenharmony_ci<p>Promise contexts may not get valid <code>triggerAsyncId</code>s by default. See
10131cb0ef41Sopenharmony_cithe section on <a href="#promise-execution-tracking">promise execution tracking</a>.</p>
10141cb0ef41Sopenharmony_ci<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>
10151cb0ef41Sopenharmony_ci<div class="api_metadata">
10161cb0ef41Sopenharmony_ci<span>Added in: v17.2.0, v16.14.0</span>
10171cb0ef41Sopenharmony_ci</div>
10181cb0ef41Sopenharmony_ci<ul>
10191cb0ef41Sopenharmony_ci<li>Returns: A map of provider types to the corresponding numeric id.
10201cb0ef41Sopenharmony_ciThis map contains all the event types that might be emitted by the <code>async_hooks.init()</code> event.</li>
10211cb0ef41Sopenharmony_ci</ul>
10221cb0ef41Sopenharmony_ci<p>This feature suppresses the deprecated usage of <code>process.binding('async_wrap').Providers</code>.
10231cb0ef41Sopenharmony_ciSee: <a href="deprecations.html#dep0111-processbinding">DEP0111</a></p>
10241cb0ef41Sopenharmony_ci</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>
10251cb0ef41Sopenharmony_ci<p>By default, promise executions are not assigned <code>asyncId</code>s due to the relatively
10261cb0ef41Sopenharmony_ciexpensive nature of the <a href="https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit">promise introspection API</a> provided by
10271cb0ef41Sopenharmony_ciV8. This means that programs using promises or <code>async</code>/<code>await</code> will not get
10281cb0ef41Sopenharmony_cicorrect execution and trigger ids for promise callback contexts by default.</p>
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ci<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>;
10311cb0ef41Sopenharmony_ci
10321cb0ef41Sopenharmony_ci<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> {
10331cb0ef41Sopenharmony_ci  <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>);
10341cb0ef41Sopenharmony_ci});
10351cb0ef41Sopenharmony_ci<span class="hljs-comment">// produces:</span>
10361cb0ef41Sopenharmony_ci<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>);
10371cb0ef41Sopenharmony_ci
10381cb0ef41Sopenharmony_ci<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> {
10391cb0ef41Sopenharmony_ci  <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>);
10401cb0ef41Sopenharmony_ci});
10411cb0ef41Sopenharmony_ci<span class="hljs-comment">// produces:</span>
10421cb0ef41Sopenharmony_ci<span class="hljs-comment">// eid 1 tid 0</span></code><button class="copy-button">copy</button></pre>
10431cb0ef41Sopenharmony_ci<p>Observe that the <code>then()</code> callback claims to have executed in the context of the
10441cb0ef41Sopenharmony_ciouter scope even though there was an asynchronous hop involved. Also,
10451cb0ef41Sopenharmony_cithe <code>triggerAsyncId</code> value is <code>0</code>, which means that we are missing context about
10461cb0ef41Sopenharmony_cithe resource that caused (triggered) the <code>then()</code> callback to be executed.</p>
10471cb0ef41Sopenharmony_ci<p>Installing async hooks via <code>async_hooks.createHook</code> enables promise execution
10481cb0ef41Sopenharmony_citracking:</p>
10491cb0ef41Sopenharmony_ci
10501cb0ef41Sopenharmony_ci<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>;
10511cb0ef41Sopenharmony_ci<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>
10521cb0ef41Sopenharmony_ci<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> {
10531cb0ef41Sopenharmony_ci  <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>);
10541cb0ef41Sopenharmony_ci});
10551cb0ef41Sopenharmony_ci<span class="hljs-comment">// produces:</span>
10561cb0ef41Sopenharmony_ci<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>);
10571cb0ef41Sopenharmony_ci
10581cb0ef41Sopenharmony_ci<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>
10591cb0ef41Sopenharmony_ci<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> {
10601cb0ef41Sopenharmony_ci  <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>);
10611cb0ef41Sopenharmony_ci});
10621cb0ef41Sopenharmony_ci<span class="hljs-comment">// produces:</span>
10631cb0ef41Sopenharmony_ci<span class="hljs-comment">// eid 7 tid 6</span></code><button class="copy-button">copy</button></pre>
10641cb0ef41Sopenharmony_ci<p>In this example, adding any actual hook function enabled the tracking of
10651cb0ef41Sopenharmony_cipromises. There are two promises in the example above; the promise created by
10661cb0ef41Sopenharmony_ci<code>Promise.resolve()</code> and the promise returned by the call to <code>then()</code>. In the
10671cb0ef41Sopenharmony_ciexample above, the first promise got the <code>asyncId</code> <code>6</code> and the latter got
10681cb0ef41Sopenharmony_ci<code>asyncId</code> <code>7</code>. During the execution of the <code>then()</code> callback, we are executing
10691cb0ef41Sopenharmony_ciin the context of promise with <code>asyncId</code> <code>7</code>. This promise was triggered by
10701cb0ef41Sopenharmony_ciasync resource <code>6</code>.</p>
10711cb0ef41Sopenharmony_ci<p>Another subtlety with promises is that <code>before</code> and <code>after</code> callbacks are run
10721cb0ef41Sopenharmony_cionly on chained promises. That means promises not created by <code>then()</code>/<code>catch()</code>
10731cb0ef41Sopenharmony_ciwill not have the <code>before</code> and <code>after</code> callbacks fired on them. For more details
10741cb0ef41Sopenharmony_cisee the details of the V8 <a href="https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit">PromiseHooks</a> API.</p>
10751cb0ef41Sopenharmony_ci</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>
10761cb0ef41Sopenharmony_ci<p>Library developers that handle their own asynchronous resources performing tasks
10771cb0ef41Sopenharmony_cilike I/O, connection pooling, or managing callback queues may use the
10781cb0ef41Sopenharmony_ci<code>AsyncResource</code> JavaScript API so that all the appropriate callbacks are called.</p>
10791cb0ef41Sopenharmony_ci<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>
10801cb0ef41Sopenharmony_ci<p>The documentation for this class has moved <a href="async_context.html#class-asyncresource"><code>AsyncResource</code></a>.</p>
10811cb0ef41Sopenharmony_ci</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>
10821cb0ef41Sopenharmony_ci<p>The documentation for this class has moved <a href="async_context.html#class-asynclocalstorage"><code>AsyncLocalStorage</code></a>.</p></section>
10831cb0ef41Sopenharmony_ci        <!-- API END -->
10841cb0ef41Sopenharmony_ci      </div>
10851cb0ef41Sopenharmony_ci    </div>
10861cb0ef41Sopenharmony_ci  </div>
10871cb0ef41Sopenharmony_ci</body>
10881cb0ef41Sopenharmony_ci</html>
1089