CINXE.COM
Node.js — The Node.js Event Loop
<!DOCTYPE html><html class="__variable_7eb81d __variable_9790f2" dir="ltr" lang="en-GB"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/><link rel="preload" href="/_next/static/media/3478b6abef19b3b3-s.p.woff2" as="font" crossorigin="" type="font/woff2"/><link rel="preload" href="/_next/static/media/3d9ea938b6afa941-s.p.woff2" as="font" crossorigin="" type="font/woff2"/><link rel="preload" href="/_next/static/media/be2416cbb012c256-s.p.woff2" as="font" crossorigin="" type="font/woff2"/><link rel="stylesheet" href="/_next/static/css/f4db1b58a3b2c635.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" data-precedence="next"/><link rel="stylesheet" href="/_next/static/css/42d9c8ef7b6b1b3d.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" data-precedence="next"/><link rel="stylesheet" href="/_next/static/css/8d845c27881b420c.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" data-precedence="next"/><link rel="stylesheet" href="/_next/static/css/4359c1bd3c47345b.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" data-precedence="next"/><link rel="stylesheet" href="/_next/static/css/018a3b9d0051f231.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" data-precedence="next"/><link rel="stylesheet" href="/_next/static/css/a9e10e7d913f3b84.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" data-precedence="next"/><link rel="preload" as="script" fetchPriority="low" href="/_next/static/chunks/webpack-30f612ad935bf165.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF"/><script src="/_next/static/chunks/87c73c54-a1961ec330a8dfd4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/2855-8b8ca695b0bea7f4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/main-app-fc20fdd0b5c1b293.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/3891-49a835c05af2af63.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/app/global-error-326a31a325d045f3.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/app/%5Blocale%5D/layout-376dd1dc37535351.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/app/%5Blocale%5D/error-1ae7101ee3725219.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script src="/_next/static/chunks/app/%5Blocale%5D/not-found-4ef8580001301355.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><meta name="next-size-adjust" content=""/><title>Node.js — The Node.js Event Loop</title><meta name="description" content="Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine."/><meta name="robots" content="index, follow"/><link rel="canonical" href="https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="x-default" href="https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="en" href="https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="es" href="https://nodejs.org/es/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="fr" href="https://nodejs.org/fr/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="id" href="https://nodejs.org/id/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="ja" href="https://nodejs.org/ja/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="ko" href="https://nodejs.org/ko/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="pt" href="https://nodejs.org/pt/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="tr" href="https://nodejs.org/tr/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="uk" href="https://nodejs.org/uk/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="zh-cn" href="https://nodejs.org/zh-cn/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" hrefLang="zh-tw" href="https://nodejs.org/zh-tw/learn/asynchronous-work/event-loop-timers-and-nexttick"/><link rel="alternate" type="application/rss+xml" href="https://nodejs.org/en/feed/blog.xml"/><meta property="og:title" content="Node.js — The Node.js Event Loop"/><meta property="og:description" content="Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine."/><meta property="og:image" content="https://nodejs.org/en/next-data/og/announcement/Node.js%20%E2%80%94%20The%20Node.js%20Event%20Loop"/><meta name="twitter:card" content="summary"/><meta name="twitter:creator" content="@nodejs"/><meta name="twitter:title" content="Node.js — The Node.js Event Loop"/><meta name="twitter:description" content="Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine."/><meta name="twitter:image" content="https://nodejs.org/static/images/logo-hexagon-card.png"/><meta name="twitter:image:alt" content="The Node.js Hexagon Logo"/><link rel="icon" href="/static/images/favicons/favicon.png"/><script src="/_next/static/chunks/polyfills-42372ed130431b0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" noModule=""></script></head><body><script>((e,t,r,n,o,a,i,l)=>{let u=document.documentElement,s=["light","dark"];function c(t){(Array.isArray(e)?e:[e]).forEach(e=>{let r="class"===e,n=r&&a?o.map(e=>a[e]||e):o;r?(u.classList.remove(...n),u.classList.add(t)):u.setAttribute(e,t)}),l&&s.includes(t)&&(u.style.colorScheme=t)}if(n)c(n);else try{let e=localStorage.getItem(t)||r,n=i&&"system"===e?window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":e;c(n)}catch(e){}})("data-theme","theme","system",null,["light","dark"],null,true,true)</script><div class="layouts_baseLayout__qEW_w"><div><nav class="NavBar_container__VTt_c"><div class="NavBar_nodeIconAndMobileItemsToggler__SAX1i"><a class="NavBar_nodeIconWrapper__tDLur" aria-label="Home" href="/en"><svg width="267" height="80" viewBox="0 0 267 80" fill="none" aria-label="Node.js logo" xmlns="http://www.w3.org/2000/svg" class="fill-[#333333] dark:fill-white NodejsLogo_nodejsLogo__mBW__"><mask id="mask0_1803_3458" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="0" y="0" width="267" height="80"><path d="M267 0H0V79.3784H267V0Z" fill="white"></path></mask><g mask="url(#mask0_1803_3458)"><path d="M234.983 78.7547C234.292 78.7547 233.648 78.5695 233.05 78.2452L226.929 74.5871C226.008 74.0776 226.469 73.8924 226.745 73.7998C227.987 73.3829 228.217 73.2903 229.506 72.5495C229.644 72.4569 229.828 72.5033 229.966 72.5959L234.66 75.4206C234.844 75.5132 235.074 75.5132 235.212 75.4206L253.575 64.7235C253.758 64.6309 253.852 64.4457 253.852 64.2143V42.8665C253.852 42.6351 253.758 42.4499 253.575 42.3573L235.212 31.7067C235.028 31.614 234.798 31.614 234.66 31.7067L216.298 42.3573C216.114 42.4499 216.022 42.6813 216.022 42.8665V64.2143C216.022 64.3995 216.114 64.6309 216.298 64.7235L221.314 67.641C224.029 69.0301 225.732 67.4094 225.732 65.7886V44.7189C225.732 44.4411 225.962 44.1633 226.285 44.1633H228.632C228.908 44.1633 229.184 44.3947 229.184 44.7189V65.7886C229.184 69.447 227.205 71.577 223.753 71.577C222.695 71.577 221.867 71.577 219.519 70.4195L214.687 67.641C213.491 66.9463 212.754 65.6498 212.754 64.2604V42.913C212.754 41.5236 213.491 40.2271 214.687 39.5324L233.05 28.8356C234.2 28.1873 235.765 28.1873 236.915 28.8356L255.278 39.5324C256.474 40.2271 257.21 41.5236 257.21 42.913V64.2604C257.21 65.6498 256.474 66.9463 255.278 67.641L236.915 78.3378C236.363 78.5695 235.673 78.7547 234.983 78.7547Z" fill="#5FA04E"></path><path d="M240.69 64.075C232.635 64.075 230.978 60.3704 230.978 57.2215C230.978 56.9437 231.208 56.6658 231.53 56.6658H233.923C234.2 56.6658 234.43 56.8511 234.43 57.1289C234.798 59.5831 235.856 60.7873 240.735 60.7873C244.601 60.7873 246.258 59.9074 246.258 57.8236C246.258 56.6197 245.796 55.7398 239.768 55.1377C234.752 54.6282 231.622 53.5169 231.622 49.4881C231.622 45.7374 234.752 43.5145 239.998 43.5145C245.89 43.5145 248.789 45.5521 249.157 49.9976C249.157 50.1366 249.111 50.2754 249.02 50.4144C248.926 50.507 248.789 50.5997 248.649 50.5997H246.21C245.982 50.5997 245.751 50.4144 245.705 50.1828C245.152 47.6359 243.725 46.8025 239.952 46.8025C235.718 46.8025 235.212 48.2842 235.212 49.3955C235.212 50.7384 235.81 51.1553 241.517 51.8962C247.177 52.637 249.847 53.7021 249.847 57.6845C249.847 61.7595 246.487 64.075 240.69 64.075Z" fill="#5FA04E"></path><path d="M267.2 41.6621C267.2 43.6071 265.588 45.2279 263.654 45.2279C261.722 45.2279 260.111 43.6533 260.111 41.6621C260.111 39.6247 261.768 38.0966 263.654 38.0966C265.542 38.0966 267.2 39.6709 267.2 41.6621ZM260.664 41.6621C260.664 43.3293 261.999 44.6722 263.608 44.6722C265.266 44.6722 266.601 43.2829 266.601 41.6621C266.601 39.9951 265.266 38.6986 263.608 38.6986C262.045 38.6986 260.664 39.9951 260.664 41.6621ZM262.321 39.6709H263.702C264.162 39.6709 265.083 39.6709 265.083 40.736C265.083 41.4769 264.621 41.6159 264.344 41.7085C264.898 41.7547 264.943 42.1254 264.989 42.6346C265.035 42.9588 265.083 43.5145 265.174 43.6997H264.344C264.344 43.5145 264.207 42.4958 264.207 42.4494C264.162 42.218 264.07 42.1254 263.794 42.1254H263.103V43.7459H262.321V39.6709ZM263.057 41.4307H263.654C264.162 41.4307 264.253 41.0603 264.253 40.875C264.253 40.3194 263.885 40.3194 263.654 40.3194H263.012V41.4307H263.057Z" fill="#5FA04E"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M43.6737 41.9539C43.6737 41.1204 43.2136 40.3334 42.4773 39.9165L23.0104 28.5712C22.6883 28.386 22.3201 28.2934 21.9519 28.2472C21.9059 28.2472 21.7679 28.2472 21.7679 28.2472C21.3997 28.2472 21.0315 28.386 20.7094 28.5712L1.19654 39.8701C0.460209 40.287 0 41.0743 0 41.9539L0.0460209 72.3315C0.0460209 72.7484 0.276126 73.165 0.644293 73.3502C1.01246 73.5819 1.47267 73.5819 1.79482 73.3502L13.3921 66.682C14.1284 66.2653 14.5886 65.4781 14.5886 64.6446V50.4282C14.5886 49.5947 15.0488 48.8074 15.7852 48.3908L20.7094 45.5197C21.0776 45.288 21.4917 45.1954 21.9059 45.1954C22.3201 45.1954 22.7343 45.288 23.0565 45.5197L27.9806 48.3908C28.7169 48.8074 29.1773 49.5947 29.1773 50.4282V64.6446C29.1773 65.4781 29.6375 66.2653 30.3738 66.682L41.8791 73.3502C42.2471 73.5819 42.7073 73.5819 43.0755 73.3502C43.4438 73.165 43.6737 72.7484 43.6737 72.3315V41.9539Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M137.465 0.138922C137.096 -0.0463072 136.636 -0.0463072 136.314 0.138922C135.946 0.370456 135.716 0.740915 135.716 1.15768V31.2573C135.716 31.5351 135.578 31.8129 135.301 31.9984C135.025 32.1372 134.749 32.1372 134.473 31.9984L129.595 29.1735C128.859 28.7569 127.984 28.7569 127.248 29.1735L107.735 40.5188C106.999 40.9357 106.539 41.7227 106.539 42.5562V65.2004C106.539 66.0342 106.999 66.8212 107.735 67.2381L127.248 78.5834C127.984 79 128.859 79 129.595 78.5834L149.108 67.2381C149.844 66.8212 150.304 66.0342 150.304 65.2004V8.75207C150.304 7.87222 149.844 7.085 149.108 6.66824L137.465 0.138922ZM135.67 57.7451C135.67 57.9765 135.578 58.1617 135.394 58.2544L128.72 62.1444C128.536 62.237 128.306 62.237 128.122 62.1444L121.449 58.2544C121.265 58.1617 121.173 57.9303 121.173 57.7451V49.9656C121.173 49.7339 121.265 49.5487 121.449 49.4561L128.122 45.5663C128.306 45.4737 128.536 45.4737 128.72 45.5663L135.394 49.4561C135.578 49.5487 135.67 49.7803 135.67 49.9656V57.7451Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M202.398 49.7801C203.135 49.3632 203.549 48.5762 203.549 47.7425V42.2319C203.549 41.3985 203.089 40.6112 202.398 40.1946L183.023 28.8957C182.287 28.4788 181.413 28.4788 180.676 28.8957L161.164 40.2407C160.427 40.6576 159.967 41.4449 159.967 42.2784V64.9226C159.967 65.7561 160.427 66.5434 161.164 66.96L180.538 78.0737C181.275 78.4905 182.149 78.4905 182.839 78.0737L194.575 71.4983C194.943 71.3131 195.173 70.8962 195.173 70.4793C195.173 70.0627 194.943 69.6459 194.575 69.4606L174.97 58.1153C174.602 57.8839 174.372 57.5135 174.372 57.0966V50.0117C174.372 49.5949 174.602 49.178 174.97 48.9928L181.091 45.4735C181.459 45.2421 181.919 45.2421 182.287 45.4735L188.408 48.9928C188.776 49.2245 189.006 49.5949 189.006 50.0117V55.5685C189.006 55.9853 189.236 56.4019 189.605 56.5872C189.973 56.8188 190.433 56.8188 190.801 56.5872L202.398 49.7801Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M181.551 48.7155C181.689 48.6228 181.874 48.6228 182.012 48.7155L185.739 50.8919C185.877 50.9845 185.969 51.1235 185.969 51.3087V55.6615C185.969 55.8468 185.877 55.9858 185.739 56.0784L182.012 58.2548C181.874 58.3474 181.689 58.3474 181.551 58.2548L177.824 56.0784C177.686 55.9858 177.594 55.8468 177.594 55.6615V51.3087C177.594 51.1235 177.686 50.9845 177.824 50.8919L181.551 48.7155Z" fill="#5FA04E"></path><path d="M76.3012 28.9882C75.5647 28.5716 74.6903 28.5716 73.954 28.9882L54.5792 40.241C53.8429 40.6578 53.4287 41.4449 53.4287 42.2786V64.83C53.4287 65.6637 53.8889 66.4507 54.5792 66.8676L73.954 78.1203C74.6903 78.5369 75.5647 78.5369 76.3012 78.1203L95.6758 66.8676C96.4123 66.4507 96.8263 65.6637 96.8263 64.83V42.2786C96.8263 41.4449 96.3662 40.6578 95.6758 40.241L76.3012 28.9882Z" fill="url(#paint0_linear_1803_3458)"></path><path d="M95.7221 40.241L76.2554 28.9882C76.0714 28.8956 75.8412 28.803 75.6572 28.7568L53.8433 66.3119C54.0273 66.5433 54.2575 66.7286 54.4874 66.8676L73.9544 78.1203C74.5067 78.4443 75.1508 78.5369 75.749 78.3517L96.2284 40.6578C96.0904 40.4726 95.9063 40.3336 95.7221 40.241Z" fill="url(#paint1_linear_1803_3458)"></path><path d="M95.7224 66.8669C96.2747 66.5427 96.6889 65.9871 96.8729 65.385L75.5651 28.7098C75.013 28.6172 74.4146 28.6633 73.9085 28.9876L54.5796 40.1939L75.427 78.4437C75.7031 78.3973 76.0255 78.3046 76.3016 78.1659L95.7224 66.8669Z" fill="url(#paint2_linear_1803_3458)"></path></g><defs><linearGradient id="paint0_linear_1803_3458" x1="83.0183" y1="37.3689" x2="65.3246" y2="73.2407" gradientUnits="userSpaceOnUse"><stop stop-color="#3F873F"></stop><stop offset="0.3296" stop-color="#3F8B3D"></stop><stop offset="0.6367" stop-color="#3E9638"></stop><stop offset="0.9341" stop-color="#3DA92E"></stop><stop offset="1" stop-color="#3DAE2B"></stop></linearGradient><linearGradient id="paint1_linear_1803_3458" x1="72.1833" y1="56.1876" x2="121.552" y2="19.937" gradientUnits="userSpaceOnUse"><stop offset="0.1376" stop-color="#3F873F"></stop><stop offset="0.4016" stop-color="#52A044"></stop><stop offset="0.7129" stop-color="#64B749"></stop><stop offset="0.9081" stop-color="#6ABF4B"></stop></linearGradient><linearGradient id="paint2_linear_1803_3458" x1="52.7188" y1="53.5463" x2="97.551" y2="53.5463" gradientUnits="userSpaceOnUse"><stop offset="0.0919165" stop-color="#6ABF4B"></stop><stop offset="0.2871" stop-color="#64B749"></stop><stop offset="0.5984" stop-color="#52A044"></stop><stop offset="0.8624" stop-color="#3F873F"></stop></linearGradient></defs></svg></a><label class="NavBar_sidebarItemTogglerLabel__9JQ0K" for="sidebarItemToggler"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon" class="NavBar_navInteractionIcon__KZ2ip"><path fill-rule="evenodd" d="M3 6.75A.75.75 0 0 1 3.75 6h16.5a.75.75 0 0 1 0 1.5H3.75A.75.75 0 0 1 3 6.75ZM3 12a.75.75 0 0 1 .75-.75h16.5a.75.75 0 0 1 0 1.5H3.75A.75.75 0 0 1 3 12Zm0 5.25a.75.75 0 0 1 .75-.75h16.5a.75.75 0 0 1 0 1.5H3.75a.75.75 0 0 1-.75-.75Z" clip-rule="evenodd"></path></svg></label></div><input class="peer NavBar_sidebarItemToggler__LXLsh" id="sidebarItemToggler" type="checkbox" aria-label="Toggle navigation menu"/><div class="NavBar_main__KU2Rx peer-checked:flex"><div class="NavBar_navItems__nTPsu"><a class="NavItem_navItem__0mE9T NavItem_nav__jCUv2 NavItem_active__P9lTF" href="/en/learn"><span class="NavItem_label__9GW08">Learn</span></a><a class="NavItem_navItem__0mE9T NavItem_nav__jCUv2" href="/en/about"><span class="NavItem_label__9GW08">About</span></a><a class="NavItem_navItem__0mE9T NavItem_nav__jCUv2" href="/en/download"><span class="NavItem_label__9GW08">Download</span></a><a class="NavItem_navItem__0mE9T NavItem_nav__jCUv2" href="/en/blog"><span class="NavItem_label__9GW08">Blog</span></a><a href="https://nodejs.org/docs/latest/api/" class="NavItem_navItem__0mE9T NavItem_nav__jCUv2"><span class="NavItem_label__9GW08">Docs</span></a><a href="https://github.com/nodejs/node/blob/main/CONTRIBUTING.md" class="NavItem_navItem__0mE9T NavItem_nav__jCUv2" target="_blank"><span class="NavItem_label__9GW08">Contribute</span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon" class="NavItem_icon__GHTPH"><path fill-rule="evenodd" d="M8.25 3.75H19.5a.75.75 0 0 1 .75.75v11.25a.75.75 0 0 1-1.5 0V6.31L5.03 20.03a.75.75 0 0 1-1.06-1.06L17.69 5.25H8.25a.75.75 0 0 1 0-1.5Z" clip-rule="evenodd"></path></svg></a><a href="https://training.linuxfoundation.org/openjs/" class="NavItem_navItem__0mE9T NavItem_nav__jCUv2" target="_blank"><span class="NavItem_label__9GW08">Certification</span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon" class="NavItem_icon__GHTPH"><path fill-rule="evenodd" d="M8.25 3.75H19.5a.75.75 0 0 1 .75.75v11.25a.75.75 0 0 1-1.5 0V6.31L5.03 20.03a.75.75 0 0 1-1.06-1.06L17.69 5.25H8.25a.75.75 0 0 1 0-1.5Z" clip-rule="evenodd"></path></svg></a></div><div class="NavBar_actionsWrapper__3ntrw"><!--$!--><template data-dgst="BAILOUT_TO_CLIENT_SIDE_RENDERING"></template><span tabindex="-1" aria-hidden="true" class="Skeleton_skeleton__HbqaM NavBar_searchButtonSkeleton__Ej9Ei" data-inline-skeleton="true"></span><!--/$--><button type="button" class="ThemeToggle_themeToggle__KbhS8" aria-label="Toggle Dark Mode"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="block dark:hidden" height="20"><path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.72 9.72 0 0 1 18 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 0 0 3 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 0 0 9.002-5.998Z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="hidden dark:block" height="20"><path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386-1.591 1.591M21 12h-2.25m-.386 6.364-1.591-1.591M12 18.75V21m-4.773-4.227-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z"></path></svg></button><button class="LanguageDropDown_languageDropdown__rrdBe" aria-label="Choose Language" type="button" id="radix-:R3n5utjfb:" aria-haspopup="menu" aria-expanded="false" data-state="closed"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" height="20"><path stroke-linecap="round" stroke-linejoin="round" d="m10.5 21 5.25-11.25L21 21m-9-3h7.5M3 5.621a48.474 48.474 0 0 1 6-.371m0 0c1.12 0 2.233.038 3.334.114M9 5.25V3m3.334 2.364C11.176 10.658 7.69 15.08 3 17.502m9.334-12.138c.896.061 1.785.147 2.666.257m-4.589 8.495a18.023 18.023 0 0 1-3.827-5.802"></path></svg></button><a href="https://github.com/nodejs/node" class="NavBar_ghIconWrapper__3PyGq" aria-label="Node.js Github"><svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M10 1.25C5.16562 1.25 1.25 5.16562 1.25 10C1.25 13.8719 3.75469 17.1422 7.23281 18.3016C7.67031 18.3781 7.83437 18.1156 7.83437 17.8859C7.83437 17.6781 7.82344 16.9891 7.82344 16.2563C5.625 16.6609 5.05625 15.7203 4.88125 15.2281C4.78281 14.9766 4.35625 14.2 3.98438 13.9922C3.67812 13.8281 3.24063 13.4234 3.97344 13.4125C4.6625 13.4016 5.15469 14.0469 5.31875 14.3094C6.10625 15.6328 7.36406 15.2609 7.86719 15.0312C7.94375 14.4625 8.17344 14.0797 8.425 13.8609C6.47813 13.6422 4.44375 12.8875 4.44375 9.54062C4.44375 8.58906 4.78281 7.80156 5.34062 7.18906C5.25313 6.97031 4.94687 6.07344 5.42812 4.87031C5.42812 4.87031 6.16094 4.64063 7.83437 5.76719C8.53438 5.57031 9.27813 5.47187 10.0219 5.47187C10.7656 5.47187 11.5094 5.57031 12.2094 5.76719C13.8828 4.62969 14.6156 4.87031 14.6156 4.87031C15.0969 6.07344 14.7906 6.97031 14.7031 7.18906C15.2609 7.80156 15.6 8.57812 15.6 9.54062C15.6 12.8984 13.5547 13.6422 11.6078 13.8609C11.925 14.1344 12.1984 14.6594 12.1984 15.4797C12.1984 16.65 12.1875 17.5906 12.1875 17.8859C12.1875 18.1156 12.3516 18.3891 12.7891 18.3016C14.5261 17.7152 16.0355 16.5988 17.1048 15.1096C18.1741 13.6204 18.7495 11.8333 18.75 10C18.75 5.16562 14.8344 1.25 10 1.25Z"></path></svg></a></div></div></nav></div><div class="layouts_articleLayout__yakmC"><nav class="ProgressionSidebar_wrapper__AU5Wu"><span class="Select_select__8U0fY Select_inline__9Ur2W"><label class="Select_label__5_LGm" for=":R2putjfb:">Change page</label><button type="button" role="combobox" aria-controls="radix-:R1aputjfb:" aria-expanded="false" aria-autocomplete="none" dir="ltr" data-state="closed" class="Select_trigger___Dfpg" id=":R2putjfb:"><span style="pointer-events:none"><span>The Node.js Event Loop</span></span><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="Select_icon__xydP_"><path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5"></path></svg></button><select aria-hidden="true" tabindex="-1" style="position:absolute;border:0;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;word-wrap:normal"></select></span><section class="ProgressionSidebarGroup_group__6Kmnd">Getting Started<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/introduction-to-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Introduction to Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/how-much-javascript-do-you-need-to-know-to-use-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>How much JavaScript do you need to know to use Node.js?</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/differences-between-nodejs-and-the-browser"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Differences between Node.js and the Browser</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/the-v8-javascript-engine"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>The V8 JavaScript Engine</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/an-introduction-to-the-npm-package-manager"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>An introduction to the npm package manager</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/ecmascript-2015-es6-and-beyond"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>ECMAScript 2015 (ES6) and beyond</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/nodejs-the-difference-between-development-and-production"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Node.js, the difference between development and production</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/nodejs-with-webassembly"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Node.js with WebAssembly</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/debugging"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Debugging Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/profiling"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Profiling Node.js Applications</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/getting-started/security-best-practices"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Security Best Practices</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">TypeScript<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/typescript/introduction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Introduction to TypeScript</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/typescript/transpile"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Running TypeScript code using transpilation</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/typescript/run"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Running TypeScript with a runner</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/typescript/run-natively"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Running TypeScript Natively</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/typescript/publishing-a-ts-package"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Publishing a TypeScript package</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">Asynchronous Work<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/asynchronous-flow-control"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Asynchronous flow control</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Overview of Blocking vs Non-Blocking</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>JavaScript Asynchronous Programming and Callbacks</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/discover-javascript-timers"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Discover JavaScript Timers</a><a class="ProgressionSidebarItem_item__yy7XA ProgressionSidebarItem_active__Q5QBT" href="/en/learn/asynchronous-work/event-loop-timers-and-nexttick"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>The Node.js Event Loop</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/the-nodejs-event-emitter"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>The Node.js Event Emitter</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/understanding-processnexttick"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Understanding process.nextTick()</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/understanding-setimmediate"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Understanding setImmediate()</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/asynchronous-work/dont-block-the-event-loop"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Don't Block the Event Loop</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">Manipulating Files<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/nodejs-file-stats"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Node.js file stats</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/nodejs-file-paths"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Node.js File Paths</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/working-with-file-descriptors-in-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Working with file descriptors in Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/reading-files-with-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Reading files with Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/writing-files-with-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Writing files with Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/working-with-folders-in-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Working with folders in Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/manipulating-files/working-with-different-filesystems"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>How to work with Different Filesystems</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">Command Line<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/command-line/run-nodejs-scripts-from-the-command-line"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Run Node.js scripts from the command line</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/command-line/how-to-read-environment-variables-from-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>How to read environment variables from Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/command-line/how-to-use-the-nodejs-repl"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>How to use the Node.js REPL</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/command-line/output-to-the-command-line-using-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Output to the command line using Node.js</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/command-line/accept-input-from-the-command-line-in-nodejs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Accept input from the command line in Node.js</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">Modules<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/modules/publishing-a-package"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Publishing a package</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/modules/publishing-node-api-modules"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>How to publish a Node-API package</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/modules/anatomy-of-an-http-transaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Anatomy of an HTTP Transaction</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/modules/abi-stability"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>ABI Stability</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/modules/how-to-use-streams"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>How to use streams</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/modules/backpressuring-in-streams"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Backpressuring in Streams</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">Diagnostics<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/diagnostics/user-journey"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>User Journey</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/diagnostics/memory"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Memory</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/diagnostics/live-debugging"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Live Debugging</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/diagnostics/poor-performance"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Poor Performance</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/diagnostics/flame-graphs"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Flame Graphs</a></div></section><section class="ProgressionSidebarGroup_group__6Kmnd">Test Runner<div class="ProgressionSidebarGroup_items__UvKlS"><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/test-runner/introduction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Discovering Node.js's test runner</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/test-runner/using-test-runner"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Using Node.js's test runner</a><a class="ProgressionSidebarItem_item__yy7XA" href="/en/learn/test-runner/mocking"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M9 3.26795L8 2.6906L7 3.26795L3.5359 5.26795L2.5359 5.8453V7V11V12.1547L3.5359 12.7321L7 14.7321L8 15.3094L9 14.7321L12.4641 12.7321L13.4641 12.1547V11V7V5.8453L12.4641 5.26795L9 3.26795Z"></path></svg>Mocking in tests</a></div></section></nav><div><main><h1 id="the-nodejs-event-loop"><a href="#the-nodejs-event-loop">The Node.js Event Loop</a></h1> <h2 id="what-is-the-event-loop"><a href="#what-is-the-event-loop">What is the Event Loop?</a></h2> <p>The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that a single JavaScript thread is used by default — by offloading operations to the system kernel whenever possible.</p> <p>Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the <strong>poll</strong> queue to eventually be executed. We'll explain this in further detail later in this topic.</p> <h2 id="event-loop-explained"><a href="#event-loop-explained">Event Loop Explained</a></h2> <p>When Node.js starts, it initializes the event loop, processes the provided input script (or drops into the <a href="https://nodejs.org/api/repl.html#repl_repl">REPL</a>, which is not covered in this document) which may make async API calls, schedule timers, or call <code>process.nextTick()</code>, then begins processing the event loop.</p> <p>The following diagram shows a simplified overview of the event loop's order of operations.</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD" tabindex="0" dir="ltr"><code style="font-family:monospace"><span class="line"><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>┌───────────────────────────┐</span></span> <span class="line"><span>┌─>│</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>timers</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>└─────────────┬─────────────┘</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>┌─────────────┴─────────────┐</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>│</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>pending</span><span> </span><span>callbacks</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>└─────────────┬─────────────┘</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>┌─────────────┴─────────────┐</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>│</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>idle,</span><span> </span><span>prepare</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>└─────────────┬─────────────┘</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>┌───────────────┐</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>┌─────────────┴─────────────┐</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span><span> </span><span></span><span> </span><span></span><span> </span><span>incoming:</span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>│</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>poll</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│<─────┤</span><span> </span><span></span><span> </span><span>connections,</span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>└─────────────┬─────────────┘</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span><span> </span><span></span><span> </span><span></span><span> </span><span>data,</span><span> </span><span>etc.</span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>┌─────────────┴─────────────┐</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>└───────────────┘</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>│</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>check</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>└─────────────┬─────────────┘</span></span> <span class="line"><span>│</span><span> </span><span></span><span> </span><span>┌─────────────┴─────────────┐</span></span> <span class="line"><span>└──┤</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>close</span><span> </span><span>callbacks</span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>│</span></span> <span class="line"><span></span><span> </span><span></span><span> </span><span></span><span> </span><span>└───────────────────────────┘</span></span> </code></pre></div> <blockquote class="Blockquote_wrapper__4GnmJ"> <p>Each box will be referred to as a "phase" of the event loop.</p> </blockquote> <p>Each phase has a FIFO queue of callbacks to execute. While each phase is special in its own way, generally, when the event loop enters a given phase, it will perform any operations specific to that phase, then execute callbacks in that phase's queue until the queue has been exhausted or the maximum number of callbacks has executed. When the queue has been exhausted or the callback limit is reached, the event loop will move to the next phase, and so on.</p> <p>Since any of these operations may schedule <em>more</em> operations and new events processed in the <strong>poll</strong> phase are queued by the kernel, poll events can be queued while polling events are being processed. As a result, long running callbacks can allow the poll phase to run much longer than a timer's threshold. See the <a href="#timers"><strong>timers</strong></a> and <a href="#poll"><strong>poll</strong></a> sections for more details.</p> <blockquote class="Blockquote_wrapper__4GnmJ"> <p>There is a slight discrepancy between the Windows and the Unix/Linux implementation, but that's not important for this demonstration. The most important parts are here. There are actually seven or eight steps, but the ones we care about — ones that Node.js actually uses - are those above.</p> </blockquote> <h2 id="phases-overview"><a href="#phases-overview">Phases Overview</a></h2> <ul> <li><strong>timers</strong>: this phase executes callbacks scheduled by <code>setTimeout()</code> and <code>setInterval()</code>.</li> <li><strong>pending callbacks</strong>: executes I/O callbacks deferred to the next loop iteration.</li> <li><strong>idle, prepare</strong>: only used internally.</li> <li><strong>poll</strong>: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and <code>setImmediate()</code>); node will block here when appropriate.</li> <li><strong>check</strong>: <code>setImmediate()</code> callbacks are invoked here.</li> <li><strong>close callbacks</strong>: some close callbacks, e.g. <code>socket.on('close', ...)</code>.</li> </ul> <p>Between each run of the event loop, Node.js checks if it is waiting for any asynchronous I/O or timers and shuts down cleanly if there are not any.</p> <h2 id="phases-in-detail"><a href="#phases-in-detail">Phases in Detail</a></h2> <h3 id="timers"><a href="#timers">timers</a></h3> <p>A timer specifies the <strong>threshold</strong> <em>after which</em> a provided callback <em>may be executed</em> rather than the <strong>exact</strong> time a person <em>wants it to be executed</em>. Timers callbacks will run as early as they can be scheduled after the specified amount of time has passed; however, Operating System scheduling or the running of other callbacks may delay them.</p> <blockquote class="Blockquote_wrapper__4GnmJ"> <p>Technically, the <a href="#poll"><strong>poll</strong> phase</a> controls when timers are executed.</p> </blockquote> <p>For example, say you schedule a timeout to execute after a 100 ms threshold, then your script starts asynchronously reading a file which takes 95 ms:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> fs</span><span style="color:#81A1C1"> =</span><span style="color:#88C0D0"> require</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">node:fs</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">function</span><span style="color:#88C0D0"> someAsyncOperation</span><span style="color:#ECEFF4">(</span><span style="color:#D8DEE9">callback</span><span style="color:#ECEFF4">)</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#707e99"> // Assume this takes 95ms to complete</span></span> <span class="line"><span style="color:#D8DEE9"> fs</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">readFile</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">/path/to/file</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#D8DEE9"> callback</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> timeoutScheduled</span><span style="color:#81A1C1"> =</span><span style="color:#D8DEE9"> Date</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">now</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#88C0D0">setTimeout</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> const</span><span style="color:#D8DEE9"> delay</span><span style="color:#81A1C1"> =</span><span style="color:#D8DEE9"> Date</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">now</span><span style="color:#D8DEE9FF">() </span><span style="color:#81A1C1">-</span><span style="color:#D8DEE9"> timeoutScheduled</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">`</span><span style="color:#81A1C1">${</span><span style="color:#D8DEE9">delay</span><span style="color:#81A1C1">}</span><span style="color:#A3BE8C">ms have passed since I was scheduled</span><span style="color:#ECEFF4">`</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">},</span><span style="color:#B48EAD"> 100</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#707e99">// do someAsyncOperation which takes 95 ms to complete</span></span> <span class="line"><span style="color:#88C0D0">someAsyncOperation</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> const</span><span style="color:#D8DEE9"> startCallback</span><span style="color:#81A1C1"> =</span><span style="color:#D8DEE9"> Date</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">now</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#707e99"> // do something that will take 10ms...</span></span> <span class="line"><span style="color:#81A1C1"> while</span><span style="color:#D8DEE9FF"> (</span><span style="color:#D8DEE9">Date</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">now</span><span style="color:#D8DEE9FF">() </span><span style="color:#81A1C1">-</span><span style="color:#D8DEE9"> startCallback</span><span style="color:#81A1C1"> <</span><span style="color:#B48EAD"> 10</span><span style="color:#D8DEE9FF">) </span><span style="color:#ECEFF4">{</span></span> <span class="line"><span style="color:#707e99"> // do nothing</span></span> <span class="line"><span style="color:#ECEFF4"> }</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>When the event loop enters the <strong>poll</strong> phase, it has an empty queue (<code>fs.readFile()</code> has not completed), so it will wait for the number of ms remaining until the soonest timer's threshold is reached. While it is waiting 95 ms pass, <code>fs.readFile()</code> finishes reading the file and its callback which takes 10 ms to complete is added to the <strong>poll</strong> queue and executed. When the callback finishes, there are no more callbacks in the queue, so the event loop will see that the threshold of the soonest timer has been reached then wrap back to the <strong>timers</strong> phase to execute the timer's callback. In this example, you will see that the total delay between the timer being scheduled and its callback being executed will be 105ms.</p> <blockquote class="Blockquote_wrapper__4GnmJ"> <p>To prevent the <strong>poll</strong> phase from starving the event loop, <a href="https://libuv.org/">libuv</a> (the C library that implements the Node.js event loop and all of the asynchronous behaviors of the platform) also has a hard maximum (system dependent) before it stops polling for more events.</p> </blockquote> <h3 id="pending-callbacks"><a href="#pending-callbacks">pending callbacks</a></h3> <p>This phase executes callbacks for some system operations such as types of TCP errors. For example if a TCP socket receives <code>ECONNREFUSED</code> when attempting to connect, some *nix systems want to wait to report the error. This will be queued to execute in the <strong>pending callbacks</strong> phase.</p> <h3 id="poll"><a href="#poll">poll</a></h3> <p>The <strong>poll</strong> phase has two main functions:</p> <ol> <li>Calculating how long it should block and poll for I/O, then</li> <li>Processing events in the <strong>poll</strong> queue.</li> </ol> <p>When the event loop enters the <strong>poll</strong> phase <em>and there are no timers scheduled</em>, one of two things will happen:</p> <ul> <li> <p><em>If the <strong>poll</strong> queue <strong>is not empty</strong></em>, the event loop will iterate through its queue of callbacks executing them synchronously until either the queue has been exhausted, or the system-dependent hard limit is reached.</p> </li> <li> <p><em>If the <strong>poll</strong> queue <strong>is empty</strong></em>, one of two more things will happen:</p> <ul> <li> <p>If scripts have been scheduled by <code>setImmediate()</code>, the event loop will end the <strong>poll</strong> phase and continue to the <strong>check</strong> phase to execute those scheduled scripts.</p> </li> <li> <p>If scripts <strong>have not</strong> been scheduled by <code>setImmediate()</code>, the event loop will wait for callbacks to be added to the queue, then execute them immediately.</p> </li> </ul> </li> </ul> <p>Once the <strong>poll</strong> queue is empty the event loop will check for timers <em>whose time thresholds have been reached</em>. If one or more timers are ready, the event loop will wrap back to the <strong>timers</strong> phase to execute those timers' callbacks.</p> <h3 id="check"><a href="#check">check</a></h3> <p>This phase allows the event loop to execute callbacks immediately after the <strong>poll</strong> phase has completed. If the <strong>poll</strong> phase becomes idle and scripts have been queued with <code>setImmediate()</code>, the event loop may continue to the <strong>check</strong> phase rather than waiting.</p> <p><code>setImmediate()</code> is actually a special timer that runs in a separate phase of the event loop. It uses a libuv API that schedules callbacks to execute after the <strong>poll</strong> phase has completed.</p> <p>Generally, as the code is executed, the event loop will eventually hit the <strong>poll</strong> phase where it will wait for an incoming connection, request, etc. However, if a callback has been scheduled with <code>setImmediate()</code> and the <strong>poll</strong> phase becomes idle, it will end and continue to the <strong>check</strong> phase rather than waiting for <strong>poll</strong> events.</p> <h3 id="close-callbacks"><a href="#close-callbacks">close callbacks</a></h3> <p>If a socket or handle is closed abruptly (e.g. <code>socket.destroy()</code>), the <code>'close'</code> event will be emitted in this phase. Otherwise it will be emitted via <code>process.nextTick()</code>.</p> <h2 id="setimmediate-vs-settimeout"><a href="#setimmediate-vs-settimeout"><code>setImmediate()</code> vs <code>setTimeout()</code></a></h2> <p><code>setImmediate()</code> and <code>setTimeout()</code> are similar, but behave in different ways depending on when they are called.</p> <ul> <li><code>setImmediate()</code> is designed to execute a script once the current <strong>poll</strong> phase completes.</li> <li><code>setTimeout()</code> schedules a script to be run after a minimum threshold in ms has elapsed.</li> </ul> <p>The order in which the timers are executed will vary depending on the context in which they are called. If both are called from within the main module, then timing will be bound by the performance of the process (which can be impacted by other applications running on the machine).</p> <p>For example, if we run the following script which is not within an I/O cycle (i.e. the main module), the order in which the two timers are executed is non-deterministic, as it is bound by the performance of the process:</p> <div dir="ltr" data-orientation="horizontal" class="Tabs_tabsRoot__iUO6B CodeTabs_root__nwOXE"><div role="tablist" aria-orientation="horizontal" class="Tabs_tabsList__7N46C" tabindex="-1" data-orientation="horizontal" style="outline:none"><button type="button" role="tab" aria-selected="true" aria-controls="radix-:R2lb9utjfb:-content-js-0" data-state="active" id="radix-:R2lb9utjfb:-trigger-js-0" class="Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ" tabindex="-1" data-orientation="horizontal" data-radix-collection-item="">JS</button><button type="button" role="tab" aria-selected="false" aria-controls="radix-:R2lb9utjfb:-content-bash-1" data-state="inactive" id="radix-:R2lb9utjfb:-trigger-bash-1" class="Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ" tabindex="-1" data-orientation="horizontal" data-radix-collection-item="">BASH</button></div><div data-state="active" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:R2lb9utjfb:-trigger-js-0" id="radix-:R2lb9utjfb:-content-js-0" tabindex="0" style="animation-duration:0s"><div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#707e99">// timeout_vs_immediate.js</span></span> <span class="line"><span style="color:#88C0D0">setTimeout</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">timeout</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">},</span><span style="color:#B48EAD"> 0</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#88C0D0">setImmediate</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">immediate</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div></div><div data-state="inactive" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:R2lb9utjfb:-trigger-bash-1" hidden="" id="radix-:R2lb9utjfb:-content-bash-1" tabindex="0"></div></div><p>However, if you move the two calls within an I/O cycle, the immediate callback is always executed first:</p> <div dir="ltr" data-orientation="horizontal" class="Tabs_tabsRoot__iUO6B CodeTabs_root__nwOXE"><div role="tablist" aria-orientation="horizontal" class="Tabs_tabsList__7N46C" tabindex="-1" data-orientation="horizontal" style="outline:none"><button type="button" role="tab" aria-selected="true" aria-controls="radix-:R2ob9utjfb:-content-js-0" data-state="active" id="radix-:R2ob9utjfb:-trigger-js-0" class="Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ" tabindex="-1" data-orientation="horizontal" data-radix-collection-item="">JS</button><button type="button" role="tab" aria-selected="false" aria-controls="radix-:R2ob9utjfb:-content-bash-1" data-state="inactive" id="radix-:R2ob9utjfb:-trigger-bash-1" class="Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ" tabindex="-1" data-orientation="horizontal" data-radix-collection-item="">BASH</button></div><div data-state="active" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:R2ob9utjfb:-trigger-js-0" id="radix-:R2ob9utjfb:-content-js-0" tabindex="0" style="animation-duration:0s"><div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#707e99">// timeout_vs_immediate.js</span></span> <span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> fs</span><span style="color:#81A1C1"> =</span><span style="color:#88C0D0"> require</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">node:fs</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D8DEE9">fs</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">readFile</span><span style="color:#D8DEE9FF">(</span><span style="color:#D8DEE9">__filename</span><span style="color:#ECEFF4">,</span><span style="color:#ECEFF4"> ()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#88C0D0"> setTimeout</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">timeout</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4"> },</span><span style="color:#B48EAD"> 0</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#88C0D0"> setImmediate</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">immediate</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4"> }</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div></div><div data-state="inactive" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:R2ob9utjfb:-trigger-bash-1" hidden="" id="radix-:R2ob9utjfb:-content-bash-1" tabindex="0"></div></div><p>The main advantage to using <code>setImmediate()</code> over <code>setTimeout()</code> is <code>setImmediate()</code> will always be executed before any timers if scheduled within an I/O cycle, independently of how many timers are present.</p> <h2 id="processnexttick"><a href="#processnexttick"><code>process.nextTick()</code></a></h2> <h3 id="understanding-processnexttick"><a href="#understanding-processnexttick">Understanding <code>process.nextTick()</code></a></h3> <p>You may have noticed that <code>process.nextTick()</code> was not displayed in the diagram, even though it's a part of the asynchronous API. This is because <code>process.nextTick()</code> is not technically part of the event loop. Instead, the <code>nextTickQueue</code> will be processed after the current operation is completed, regardless of the current phase of the event loop. Here, an <em>operation</em> is defined as a transition from the underlying C/C++ handler, and handling the JavaScript that needs to be executed.</p> <p>Looking back at our diagram, any time you call <code>process.nextTick()</code> in a given phase, all callbacks passed to <code>process.nextTick()</code> will be resolved before the event loop continues. This can create some bad situations because <strong>it allows you to "starve" your I/O by making recursive <code>process.nextTick()</code> calls</strong>, which prevents the event loop from reaching the <strong>poll</strong> phase.</p> <h3 id="why-would-that-be-allowed"><a href="#why-would-that-be-allowed">Why would that be allowed?</a></h3> <p>Why would something like this be included in Node.js? Part of it is a design philosophy where an API should always be asynchronous even where it doesn't have to be. Take this code snippet for example:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">function</span><span style="color:#88C0D0"> apiCall</span><span style="color:#ECEFF4">(</span><span style="color:#D8DEE9">arg</span><span style="color:#ECEFF4">,</span><span style="color:#D8DEE9"> callback</span><span style="color:#ECEFF4">)</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> if</span><span style="color:#D8DEE9FF"> (</span><span style="color:#81A1C1">typeof</span><span style="color:#D8DEE9"> arg</span><span style="color:#81A1C1"> !==</span><span style="color:#ECEFF4"> '</span><span style="color:#A3BE8C">string</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span></span> <span class="line"><span style="color:#81A1C1"> return</span><span style="color:#D8DEE9"> process</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">nextTick</span><span style="color:#D8DEE9FF">(</span></span> <span class="line"><span style="color:#D8DEE9"> callback</span><span style="color:#ECEFF4">,</span></span> <span class="line"><span style="color:#81A1C1"> new</span><span style="color:#88C0D0"> TypeError</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">argument should be string</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span></span> <span class="line"><span style="color:#D8DEE9FF"> )</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>The snippet does an argument check and if it's not correct, it will pass the error to the callback. The API updated fairly recently to allow passing arguments to <code>process.nextTick()</code> allowing it to take any arguments passed after the callback to be propagated as the arguments to the callback so you don't have to nest functions.</p> <p>What we're doing is passing an error back to the user but only <em>after</em> we have allowed the rest of the user's code to execute. By using <code>process.nextTick()</code> we guarantee that <code>apiCall()</code> always runs its callback <em>after</em> the rest of the user's code and <em>before</em> the event loop is allowed to proceed. To achieve this, the JS call stack is allowed to unwind then immediately execute the provided callback which allows a person to make recursive calls to <code>process.nextTick()</code> without reaching a <code>RangeError: Maximum call stack size exceeded from v8</code>.</p> <p>This philosophy can lead to some potentially problematic situations. Take this snippet for example:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">let</span><span style="color:#D8DEE9"> bar</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#707e99">// this has an asynchronous signature, but calls callback synchronously</span></span> <span class="line"><span style="color:#81A1C1">function</span><span style="color:#88C0D0"> someAsyncApiCall</span><span style="color:#ECEFF4">(</span><span style="color:#D8DEE9">callback</span><span style="color:#ECEFF4">)</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#88C0D0"> callback</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span></span> <span class="line"></span> <span class="line"><span style="color:#707e99">// the callback is called before `someAsyncApiCall` completes.</span></span> <span class="line"><span style="color:#88C0D0">someAsyncApiCall</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#707e99"> // since someAsyncApiCall hasn't completed, bar hasn't been assigned any value</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">bar</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#D8DEE9"> bar</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span><span style="color:#707e99"> // undefined</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D8DEE9">bar</span><span style="color:#81A1C1"> =</span><span style="color:#B48EAD"> 1</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>The user defines <code>someAsyncApiCall()</code> to have an asynchronous signature, but it actually operates synchronously. When it is called, the callback provided to <code>someAsyncApiCall()</code> is called in the same phase of the event loop because <code>someAsyncApiCall()</code> doesn't actually do anything asynchronously. As a result, the callback tries to reference <code>bar</code> even though it may not have that variable in scope yet, because the script has not been able to run to completion.</p> <p>By placing the callback in a <code>process.nextTick()</code>, the script still has the ability to run to completion, allowing all the variables, functions, etc., to be initialized prior to the callback being called. It also has the advantage of not allowing the event loop to continue. It may be useful for the user to be alerted to an error before the event loop is allowed to continue. Here is the previous example using <code>process.nextTick()</code>:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">let</span><span style="color:#D8DEE9"> bar</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">function</span><span style="color:#88C0D0"> someAsyncApiCall</span><span style="color:#ECEFF4">(</span><span style="color:#D8DEE9">callback</span><span style="color:#ECEFF4">)</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> process</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">nextTick</span><span style="color:#D8DEE9FF">(</span><span style="color:#D8DEE9">callback</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span></span> <span class="line"></span> <span class="line"><span style="color:#88C0D0">someAsyncApiCall</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">bar</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#D8DEE9"> bar</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span><span style="color:#707e99"> // 1</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D8DEE9">bar</span><span style="color:#81A1C1"> =</span><span style="color:#B48EAD"> 1</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>Here's another real world example:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> server</span><span style="color:#81A1C1"> =</span><span style="color:#D8DEE9"> net</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">createServer</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {}</span><span style="color:#D8DEE9FF">)</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">listen</span><span style="color:#D8DEE9FF">(</span><span style="color:#B48EAD">8080</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D8DEE9">server</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">on</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">listening</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#ECEFF4"> ()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>When only a port is passed, the port is bound immediately. So, the <code>'listening'</code> callback could be called immediately. The problem is that the <code>.on('listening')</code> callback will not have been set by that time.</p> <p>To get around this, the <code>'listening'</code> event is queued in a <code>nextTick()</code> to allow the script to run to completion. This allows the user to set any event handlers they want.</p> <h2 id="processnexttick-vs-setimmediate"><a href="#processnexttick-vs-setimmediate"><code>process.nextTick()</code> vs <code>setImmediate()</code></a></h2> <p>We have two calls that are similar as far as users are concerned, but their names are confusing.</p> <ul> <li><code>process.nextTick()</code> fires immediately on the same phase</li> <li><code>setImmediate()</code> fires on the following iteration or 'tick' of the event loop</li> </ul> <p>In essence, the names should be swapped. <code>process.nextTick()</code> fires more immediately than <code>setImmediate()</code>, but this is an artifact of the past which is unlikely to change. Making this switch would break a large percentage of the packages on npm. Every day more new modules are being added, which means every day we wait, more potential breakages occur. While they are confusing, the names themselves won't change.</p> <blockquote class="Blockquote_wrapper__4GnmJ"> <p>We recommend developers use <code>setImmediate()</code> in all cases because it's easier to reason about.</p> </blockquote> <h2 id="why-use-processnexttick"><a href="#why-use-processnexttick">Why use <code>process.nextTick()</code>?</a></h2> <p>There are two main reasons:</p> <ol> <li> <p>Allow users to handle errors, cleanup any then unneeded resources, or perhaps try the request again before the event loop continues.</p> </li> <li> <p>At times it's necessary to allow a callback to run after the call stack has unwound but before the event loop continues.</p> </li> </ol> <p>One example is to match the user's expectations. Simple example:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> server</span><span style="color:#81A1C1"> =</span><span style="color:#D8DEE9"> net</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">createServer</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#D8DEE9">server</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">on</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">connection</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#D8DEE9"> conn</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#D8DEE9">server</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">listen</span><span style="color:#D8DEE9FF">(</span><span style="color:#B48EAD">8080</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#D8DEE9">server</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">on</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">listening</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#ECEFF4"> ()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>Say that <code>listen()</code> is run at the beginning of the event loop, but the listening callback is placed in a <code>setImmediate()</code>. Unless a hostname is passed, binding to the port will happen immediately. For the event loop to proceed, it must hit the <strong>poll</strong> phase, which means there is a non-zero chance that a connection could have been received allowing the connection event to be fired before the listening event.</p> <p>Another example is extending an <code>EventEmitter</code> and emitting an event from within the constructor:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> EventEmitter</span><span style="color:#81A1C1"> =</span><span style="color:#88C0D0"> require</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">node:events</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">class</span><span style="color:#8FBCBB"> MyEmitter</span><span style="color:#81A1C1"> extends</span><span style="color:#8FBCBB;font-weight:bold"> EventEmitter</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> constructor</span><span style="color:#ECEFF4">()</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> super</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#81A1C1"> this</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">emit</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">event</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4"> }</span></span> <span class="line"><span style="color:#ECEFF4">}</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> myEmitter</span><span style="color:#81A1C1"> =</span><span style="color:#81A1C1"> new</span><span style="color:#88C0D0"> MyEmitter</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#D8DEE9">myEmitter</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">on</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">event</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#ECEFF4"> ()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">an event occurred!</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div> <p>You can't emit an event from the constructor immediately because the script will not have processed to the point where the user assigns a callback to that event. So, within the constructor itself, you can use <code>process.nextTick()</code> to set a callback to emit the event after the constructor has finished, which provides the expected results:</p> <div class="CodeBox_root__yVRmc"><pre class="CodeBox_content__3WjnD shiki nord language-js" tabindex="0" dir="ltr"><code><span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> EventEmitter</span><span style="color:#81A1C1"> =</span><span style="color:#88C0D0"> require</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">node:events</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">class</span><span style="color:#8FBCBB"> MyEmitter</span><span style="color:#81A1C1"> extends</span><span style="color:#8FBCBB;font-weight:bold"> EventEmitter</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> constructor</span><span style="color:#ECEFF4">()</span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> super</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"></span> <span class="line"><span style="color:#707e99"> // use nextTick to emit the event once a handler is assigned</span></span> <span class="line"><span style="color:#D8DEE9"> process</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">nextTick</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#81A1C1"> this</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">emit</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">event</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4"> }</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4"> }</span></span> <span class="line"><span style="color:#ECEFF4">}</span></span> <span class="line"></span> <span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> myEmitter</span><span style="color:#81A1C1"> =</span><span style="color:#81A1C1"> new</span><span style="color:#88C0D0"> MyEmitter</span><span style="color:#D8DEE9FF">()</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#D8DEE9">myEmitter</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">on</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">event</span><span style="color:#ECEFF4">'</span><span style="color:#ECEFF4">,</span><span style="color:#ECEFF4"> ()</span><span style="color:#81A1C1"> =></span><span style="color:#ECEFF4"> {</span></span> <span class="line"><span style="color:#D8DEE9"> console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">'</span><span style="color:#A3BE8C">an event occurred!</span><span style="color:#ECEFF4">'</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"><span style="color:#ECEFF4">}</span><span style="color:#D8DEE9FF">)</span><span style="color:#81A1C1">;</span></span> <span class="line"></span></code></pre><div class="CodeBox_footer__vEel8"><span class="CodeBox_language___7Wpp">JavaScript</span><button class="Button_button__CewwK Button_neutral__qAV56 CodeBox_action__v9xMg" type="button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="CodeBox_icon__pCRVM"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"></path></svg>Copy to clipboard</button></div></div><div class="mt-4 grid w-full grid-cols-1 gap-4 md:grid-cols-2"><a class="CrossLink_crossLink__OH1_N" href="/en/learn/asynchronous-work/discover-javascript-timers"><span class="CrossLink_header__opkEd"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon" class="CrossLink_icon__Tm_uZ"><path fill-rule="evenodd" d="M7.72 12.53a.75.75 0 0 1 0-1.06l7.5-7.5a.75.75 0 1 1 1.06 1.06L9.31 12l6.97 6.97a.75.75 0 1 1-1.06 1.06l-7.5-7.5Z" clip-rule="evenodd"></path></svg>Prev</span><span class="CrossLink_content__AuXgK">Discover JavaScript Timers</span></a><a class="CrossLink_crossLink__OH1_N" href="/en/learn/asynchronous-work/the-nodejs-event-emitter"><span class="CrossLink_header__opkEd CrossLink_reverse__CVtKH"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon" class="CrossLink_icon__Tm_uZ"><path fill-rule="evenodd" d="M16.28 11.47a.75.75 0 0 1 0 1.06l-7.5 7.5a.75.75 0 0 1-1.06-1.06L14.69 12 7.72 5.03a.75.75 0 0 1 1.06-1.06l7.5 7.5Z" clip-rule="evenodd"></path></svg>Next</span><span class="CrossLink_content__AuXgK CrossLink_reverse__CVtKH">The Node.js Event Emitter</span></a></div></main><div class="MetaBar_wrapper__Th_dl"><dl><dt>Reading Time</dt><dd>13 min read</dd><dt>Contribute</dt><dd><svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="fill-neutral-700 dark:fill-neutral-100"><path d="M10 1.25C5.16562 1.25 1.25 5.16562 1.25 10C1.25 13.8719 3.75469 17.1422 7.23281 18.3016C7.67031 18.3781 7.83437 18.1156 7.83437 17.8859C7.83437 17.6781 7.82344 16.9891 7.82344 16.2563C5.625 16.6609 5.05625 15.7203 4.88125 15.2281C4.78281 14.9766 4.35625 14.2 3.98438 13.9922C3.67812 13.8281 3.24063 13.4234 3.97344 13.4125C4.6625 13.4016 5.15469 14.0469 5.31875 14.3094C6.10625 15.6328 7.36406 15.2609 7.86719 15.0312C7.94375 14.4625 8.17344 14.0797 8.425 13.8609C6.47813 13.6422 4.44375 12.8875 4.44375 9.54062C4.44375 8.58906 4.78281 7.80156 5.34062 7.18906C5.25313 6.97031 4.94687 6.07344 5.42812 4.87031C5.42812 4.87031 6.16094 4.64063 7.83437 5.76719C8.53438 5.57031 9.27813 5.47187 10.0219 5.47187C10.7656 5.47187 11.5094 5.57031 12.2094 5.76719C13.8828 4.62969 14.6156 4.87031 14.6156 4.87031C15.0969 6.07344 14.7906 6.97031 14.7031 7.18906C15.2609 7.80156 15.6 8.57812 15.6 9.54062C15.6 12.8984 13.5547 13.6422 11.6078 13.8609C11.925 14.1344 12.1984 14.6594 12.1984 15.4797C12.1984 16.65 12.1875 17.5906 12.1875 17.8859C12.1875 18.1156 12.3516 18.3891 12.7891 18.3016C14.5261 17.7152 16.0355 16.5988 17.1048 15.1096C18.1741 13.6204 18.7495 11.8333 18.75 10C18.75 5.16562 14.8344 1.25 10 1.25Z"></path></svg><a href="https://github.com/nodejs/nodejs.org/blob/main/apps/site/pages/en/learn/asynchronous-work/event-loop-timers-and-nexttick.md">Edit this page</a></dd><dt>Table of Contents</dt><dd><ol><li class=""><a href="#what-is-the-event-loop"> <!-- -->What is the Event Loop?</a></li><li class=""><a href="#event-loop-explained"> <!-- -->Event Loop Explained</a></li><li class=""><a href="#phases-overview"> <!-- -->Phases Overview</a></li><li class=""><a href="#phases-in-detail"> <!-- -->Phases in Detail</a></li><li class="pl-2"><a href="#timers"> <!-- -->timers</a></li><li class="pl-2"><a href="#pending-callbacks"> <!-- -->pending callbacks</a></li><li class="pl-2"><a href="#poll"> <!-- -->poll</a></li><li class="pl-2"><a href="#check"> <!-- -->check</a></li><li class="pl-2"><a href="#close-callbacks"> <!-- -->close callbacks</a></li><li class=""><a href="#setimmediate-vs-settimeout"> <!-- -->setImmediate() vs setTimeout()</a></li><li class=""><a href="#processnexttick"> <!-- -->process.nextTick()</a></li><li class="pl-2"><a href="#understanding-processnexttick"> <!-- -->Understanding process.nextTick()</a></li><li class="pl-2"><a href="#why-would-that-be-allowed"> <!-- -->Why would that be allowed?</a></li><li class=""><a href="#processnexttick-vs-setimmediate"> <!-- -->process.nextTick() vs setImmediate()</a></li><li class=""><a href="#why-use-processnexttick"> <!-- -->Why use process.nextTick()?</a></li></ol></dd></dl></div></div><nav aria-label="breadcrumb"><ol itemScope="" itemType="https://schema.org/BreadcrumbList" class="BreadcrumbRoot_list__hVu90"><li itemProp="itemListElement" itemScope="" itemType="https://schema.org/ListItem" class="BreadcrumbItem_item__4zgao"><a itemScope="" itemType="http://schema.org/Thing" itemProp="item" itemID="/" class="BreadcrumbLink_link__epqFl" aria-label="Navigate to Home" href="/en"><span itemProp="name"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" aria-label="Navigate to Home" class="BreadcrumbHomeLink_icon__LwLYs"><title>Navigate to Home</title><path stroke-linecap="round" stroke-linejoin="round" d="m2.25 12 8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"></path></svg></span></a><meta itemProp="position" content="1"/><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="BreadcrumbItem_separator__KK1AY"><path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5"></path></svg></li><li itemProp="itemListElement" itemScope="" itemType="https://schema.org/ListItem" class="BreadcrumbItem_item__4zgao"><a href="" itemScope="" itemType="http://schema.org/Thing" itemProp="item" itemID="" class="BreadcrumbLink_link__epqFl"><span itemProp="name">Asynchronous Work</span></a><meta itemProp="position" content="2"/><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon" class="BreadcrumbItem_separator__KK1AY"><path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5"></path></svg></li><li itemProp="itemListElement" itemScope="" itemType="https://schema.org/ListItem" class="BreadcrumbItem_item__4zgao"><a itemScope="" itemType="http://schema.org/Thing" itemProp="item" itemID="/learn/asynchronous-work/event-loop-timers-and-nexttick" class="BreadcrumbLink_link__epqFl BreadcrumbLink_active__Yy6px" aria-current="page" href="/en/learn/asynchronous-work/event-loop-timers-and-nexttick"><span itemProp="name">The Node.js Event Loop</span></a><meta itemProp="position" content="3"/></li></ol></nav></div><footer class="Footer_footer__7ZVsN"><div class="Footer_sectionPrimary__6nfYa"><a href="https://trademark-policy.openjsf.org/" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08">Trademark Policy</span></a><a href="https://privacy-policy.openjsf.org/" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08">Privacy Policy</span></a><a class="NavItem_navItem__0mE9T NavItem_footer__pMnwS" href="/en/about/previous-releases"><span class="NavItem_label__9GW08">Version Support</span></a><a href="https://github.com/openjs-foundation/cross-project-council/blob/main/CODE_OF_CONDUCT.md" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08">Code of Conduct</span></a><a href="https://github.com/nodejs/node/security/policy" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08">Security Policy</span></a></div><div class="Footer_sectionSecondary__e8pQF"><a href="https://openjsf.org/" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08">© <!-- -->OpenJS Foundation</span></a><div class="Footer_social__fgBIS"><a href="https://github.com/nodejs/node" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08"><svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-label="https://github.com/nodejs/node"><path d="M10 1.25C5.16562 1.25 1.25 5.16562 1.25 10C1.25 13.8719 3.75469 17.1422 7.23281 18.3016C7.67031 18.3781 7.83437 18.1156 7.83437 17.8859C7.83437 17.6781 7.82344 16.9891 7.82344 16.2563C5.625 16.6609 5.05625 15.7203 4.88125 15.2281C4.78281 14.9766 4.35625 14.2 3.98438 13.9922C3.67812 13.8281 3.24063 13.4234 3.97344 13.4125C4.6625 13.4016 5.15469 14.0469 5.31875 14.3094C6.10625 15.6328 7.36406 15.2609 7.86719 15.0312C7.94375 14.4625 8.17344 14.0797 8.425 13.8609C6.47813 13.6422 4.44375 12.8875 4.44375 9.54062C4.44375 8.58906 4.78281 7.80156 5.34062 7.18906C5.25313 6.97031 4.94687 6.07344 5.42812 4.87031C5.42812 4.87031 6.16094 4.64063 7.83437 5.76719C8.53438 5.57031 9.27813 5.47187 10.0219 5.47187C10.7656 5.47187 11.5094 5.57031 12.2094 5.76719C13.8828 4.62969 14.6156 4.87031 14.6156 4.87031C15.0969 6.07344 14.7906 6.97031 14.7031 7.18906C15.2609 7.80156 15.6 8.57812 15.6 9.54062C15.6 12.8984 13.5547 13.6422 11.6078 13.8609C11.925 14.1344 12.1984 14.6594 12.1984 15.4797C12.1984 16.65 12.1875 17.5906 12.1875 17.8859C12.1875 18.1156 12.3516 18.3891 12.7891 18.3016C14.5261 17.7152 16.0355 16.5988 17.1048 15.1096C18.1741 13.6204 18.7495 11.8333 18.75 10C18.75 5.16562 14.8344 1.25 10 1.25Z"></path></svg></span></a><a href="https://social.lfx.dev/@nodejs" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08"><svg width="20" height="20" viewBox="0 0 75 79" fill="none" xmlns="http://www.w3.org/2000/svg" aria-label="https://social.lfx.dev/@nodejs"><path d="M73.8393 17.4898C72.6973 9.00165 65.2994 2.31235 56.5296 1.01614C55.05 0.797115 49.4441 0 36.4582 0H36.3612C23.3717 0 20.585 0.797115 19.1054 1.01614C10.5798 2.27644 2.79399 8.28712 0.904997 16.8758C-0.00358524 21.1056 -0.100549 25.7949 0.0682394 30.0965C0.308852 36.2651 0.355538 42.423 0.91577 48.5665C1.30307 52.6474 1.97872 56.6957 2.93763 60.6812C4.73325 68.042 12.0019 74.1676 19.1233 76.6666C26.7478 79.2728 34.9474 79.7055 42.8039 77.9162C43.6682 77.7151 44.5217 77.4817 45.3645 77.216C47.275 76.6092 49.5123 75.9305 51.1571 74.7385C51.1797 74.7217 51.1982 74.7001 51.2112 74.6753C51.2243 74.6504 51.2316 74.6229 51.2325 74.5948V68.6416C51.2321 68.6154 51.2259 68.5896 51.2142 68.5661C51.2025 68.5426 51.1858 68.522 51.1651 68.5058C51.1444 68.4896 51.1204 68.4783 51.0948 68.4726C51.0692 68.4669 51.0426 68.467 51.0171 68.4729C45.9835 69.675 40.8254 70.2777 35.6502 70.2682C26.7439 70.2682 24.3486 66.042 23.6626 64.2826C23.1113 62.762 22.7612 61.1759 22.6212 59.5646C22.6197 59.5375 22.6247 59.5105 22.6357 59.4857C22.6466 59.4609 22.6633 59.4391 22.6843 59.422C22.7053 59.4048 22.73 59.3929 22.7565 59.3871C22.783 59.3813 22.8104 59.3818 22.8367 59.3886C27.7864 60.5826 32.8604 61.1853 37.9522 61.1839C39.1768 61.1839 40.3978 61.1839 41.6224 61.1516C46.7435 61.008 52.1411 60.7459 57.1796 59.7621C57.3053 59.7369 57.431 59.7154 57.5387 59.6831C65.4861 58.157 73.0493 53.3672 73.8178 41.2381C73.8465 40.7606 73.9184 36.2364 73.9184 35.7409C73.9219 34.0569 74.4606 23.7949 73.8393 17.4898Z" fill="url(#paint0_linear_549_34)"></path><path d="M61.2484 27.0263V48.114H52.8916V27.6475C52.8916 23.3388 51.096 21.1413 47.4437 21.1413C43.4287 21.1413 41.4177 23.7409 41.4177 28.8755V40.0782H33.1111V28.8755C33.1111 23.7409 31.0965 21.1413 27.0815 21.1413C23.4507 21.1413 21.6371 23.3388 21.6371 27.6475V48.114H13.2839V27.0263C13.2839 22.7176 14.384 19.2946 16.5843 16.7572C18.8539 14.2258 21.8311 12.926 25.5264 12.926C29.8036 12.926 33.0357 14.5705 35.1905 17.8559L37.2698 21.346L39.3527 17.8559C41.5074 14.5705 44.7395 12.926 49.0095 12.926C52.7013 12.926 55.6784 14.2258 57.9553 16.7572C60.1531 19.2922 61.2508 22.7152 61.2484 27.0263Z" fill="white"></path><defs><linearGradient id="paint0_linear_549_34" x1="37.0692" y1="0" x2="37.0692" y2="79" gradientUnits="userSpaceOnUse"><stop stop-color="#6364FF"></stop><stop offset="1" stop-color="#563ACC"></stop></linearGradient></defs></svg></span></a><a href="https://twitter.com/nodejs" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08"><svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-label="https://twitter.com/nodejs"><path d="M18.901 1.153h3.68l-8.04 9.19L24 22.846h-7.406l-5.8-7.584-6.638 7.584H.474l8.6-9.83L0 1.154h7.594l5.243 6.932ZM17.61 20.644h2.039L6.486 3.24H4.298Z"></path></svg></span></a><a href="https://bsky.app/profile/nodejs.org" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08"><svg width="20" height="20" viewBox="0 0 568 501" fill="none" xmlns="http://www.w3.org/2000/svg" aria-label="https://bsky.app/profile/nodejs.org"><path fill="#1185FE" d="M123.121 33.664C188.241 82.553 258.281 181.68 284 234.873c25.719-53.192 95.759-152.32 160.879-201.21C491.866-1.611 568-28.906 568 57.947c0 17.346-9.945 145.713-15.778 166.555-20.275 72.453-94.155 90.933-159.875 79.748C507.222 323.8 536.444 388.56 473.333 453.32c-119.86 122.992-172.272-30.859-185.702-70.281-2.462-7.227-3.614-10.608-3.631-7.733-.017-2.875-1.169.506-3.631 7.733-13.43 39.422-65.842 193.273-185.702 70.281-63.111-64.76-33.89-129.52 80.986-149.071-65.72 11.185-139.6-7.295-159.875-79.748C9.945 203.659 0 75.291 0 57.946 0-28.906 76.135-1.612 123.121 33.664Z"></path></svg></span></a><a href="https://slack-invite.openjsf.org/" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08"><svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" aria-label="https://slack-invite.openjsf.org/"><path fill="#2EB67D" d="M16.563 9.375A1.562 1.562 0 1 0 15 7.813v1.562h1.563Zm-4.375 0c.862 0 1.562-.7 1.562-1.563V3.438a1.563 1.563 0 0 0-3.125 0v4.374c0 .863.7 1.563 1.563 1.563Z"></path><path fill="#E01E5A" d="M3.437 10.625A1.562 1.562 0 1 0 5 12.187v-1.562H3.437Zm4.376 0c-.863 0-1.563.7-1.563 1.563v4.374a1.563 1.563 0 0 0 3.125 0v-4.374c0-.863-.7-1.563-1.563-1.563Z"></path><path fill="#ECB22E" d="M10.625 16.563A1.562 1.562 0 1 0 12.187 15h-1.562v1.563Zm0-4.375c0 .862.7 1.562 1.563 1.562h4.374a1.562 1.562 0 1 0 0-3.125h-4.374c-.863 0-1.563.7-1.563 1.563Z"></path><path fill="#36C5F0" d="M9.375 3.437A1.562 1.562 0 1 0 7.813 5h1.562V3.437Zm0 4.376c0-.863-.7-1.563-1.563-1.563H3.438a1.563 1.563 0 0 0 0 3.125h4.374c.863 0 1.563-.7 1.563-1.563Z"></path></svg></span></a><a href="https://www.linkedin.com/company/node-js" class="NavItem_navItem__0mE9T NavItem_footer__pMnwS"><span class="NavItem_label__9GW08"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20" aria-label="https://www.linkedin.com/company/node-js"><path d="M20.5 2h-17A1.5 1.5 0 002 3.5v17A1.5 1.5 0 003.5 22h17a1.5 1.5 0 001.5-1.5v-17A1.5 1.5 0 0020.5 2zM8 19H5v-9h3zM6.5 8.25A1.75 1.75 0 118.3 6.5a1.78 1.78 0 01-1.8 1.75zM19 19h-3v-4.74c0-1.42-.6-1.93-1.38-1.93A1.74 1.74 0 0013 14.19a.66.66 0 000 .14V19h-3v-9h2.9v1.3a3.11 3.11 0 012.7-1.4c1.55 0 3.36.86 3.36 3.66z"></path></svg></span></a></div></div></footer></div><div role="region" aria-label="Notifications (F8)" tabindex="-1" style="pointer-events:none"><ol tabindex="-1" class="fixed bottom-0 right-0 list-none"></ol></div><a rel="me" aria-hidden="true" class="hidden" href="https://social.lfx.dev/@nodejs"></a><!--$--><!--/$--><script src="/_next/static/chunks/webpack-30f612ad935bf165.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0])</script><script>self.__next_f.push([1,"1:\"$Sreact.fragment\"\n2:I[49507,[],\"\"]\n3:I[52639,[],\"\"]\n6:I[91432,[],\"OutletBoundary\"]\n8:I[91432,[],\"MetadataBoundary\"]\na:I[91432,[],\"ViewportBoundary\"]\nc:I[76087,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3891\",\"static/chunks/3891-49a835c05af2af63.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4219\",\"static/chunks/app/global-error-326a31a325d045f3.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n:HL[\"/_next/static/media/3478b6abef19b3b3-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n:HL[\"/_next/static/media/3d9ea938b6afa941-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n:HL[\"/_next/static/media/be2416cbb012c256-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n:HL[\"/_next/static/css/f4db1b58a3b2c635.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"style\"]\n:HL[\"/_next/static/css/42d9c8ef7b6b1b3d.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"style\"]\n:HL[\"/_next/static/css/8d845c27881b420c.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"style\"]\n:HL[\"/_next/static/css/4359c1bd3c47345b.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"style\"]\n:HL[\"/_next/static/css/018a3b9d0051f231.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"style\"]\n:HL[\"/_next/static/css/a9e10e7d913f3b84.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"style\"]\n"])</script><script>self.__next_f.push([1,"0:{\"P\":null,\"b\":\"9oNtuy9khUkb0a5Zd-tKO\",\"p\":\"\",\"c\":[\"\",\"en\",\"learn\",\"asynchronous-work\",\"event-loop-timers-and-nexttick\"],\"i\":false,\"f\":[[[\"\",{\"children\":[[\"locale\",\"en\",\"d\"],{\"children\":[[\"path\",\"learn/asynchronous-work/event-loop-timers-and-nexttick\",\"c\"],{\"children\":[\"__PAGE__\",{}]}]},\"$undefined\",\"$undefined\",true]}],[\"\",[\"$\",\"$1\",\"c\",{\"children\":[null,[\"$\",\"$L2\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L3\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[],[[\"$\",\"title\",null,{\"children\":\"404: This page could not be found.\"}],[\"$\",\"div\",null,{\"style\":{\"fontFamily\":\"system-ui,\\\"Segoe UI\\\",Roboto,Helvetica,Arial,sans-serif,\\\"Apple Color Emoji\\\",\\\"Segoe UI Emoji\\\"\",\"height\":\"100vh\",\"textAlign\":\"center\",\"display\":\"flex\",\"flexDirection\":\"column\",\"alignItems\":\"center\",\"justifyContent\":\"center\"},\"children\":[\"$\",\"div\",null,{\"children\":[[\"$\",\"style\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}\"}}],[\"$\",\"h1\",null,{\"className\":\"next-error-h1\",\"style\":{\"display\":\"inline-block\",\"margin\":\"0 20px 0 0\",\"padding\":\"0 23px 0 0\",\"fontSize\":24,\"fontWeight\":500,\"verticalAlign\":\"top\",\"lineHeight\":\"49px\"},\"children\":404}],[\"$\",\"div\",null,{\"style\":{\"display\":\"inline-block\"},\"children\":[\"$\",\"h2\",null,{\"style\":{\"fontSize\":14,\"fontWeight\":400,\"lineHeight\":\"49px\",\"margin\":0},\"children\":\"This page could not be found.\"}]}]]}]}]]],\"forbidden\":\"$undefined\",\"unauthorized\":\"$undefined\"}]]}],{\"children\":[[\"locale\",\"en\",\"d\"],[\"$\",\"$1\",\"c\",{\"children\":[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/f4db1b58a3b2c635.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/42d9c8ef7b6b1b3d.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}],[\"$\",\"link\",\"2\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/8d845c27881b420c.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}],[\"$\",\"link\",\"3\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/4359c1bd3c47345b.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]],\"$L4\"]}],{\"children\":[[\"path\",\"learn/asynchronous-work/event-loop-timers-and-nexttick\",\"c\"],[\"$\",\"$1\",\"c\",{\"children\":[null,[\"$\",\"$L2\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$0:f:0:1:2:children:0\",\"children\",\"$0:f:0:1:2:children:2:children:0\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L3\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"forbidden\":\"$undefined\",\"unauthorized\":\"$undefined\"}]]}],{\"children\":[\"__PAGE__\",[\"$\",\"$1\",\"c\",{\"children\":[\"$L5\",[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/018a3b9d0051f231.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/a9e10e7d913f3b84.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]],[\"$\",\"$L6\",null,{\"children\":\"$L7\"}]]}],{},null,false]},null,false]},null,false]},null,false],[\"$\",\"$1\",\"h\",{\"children\":[null,[\"$\",\"$1\",\"3eUua8f5mP6epM5xQsIQB\",{\"children\":[[\"$\",\"$L8\",null,{\"children\":\"$L9\"}],[\"$\",\"$La\",null,{\"children\":\"$Lb\"}],[\"$\",\"meta\",null,{\"name\":\"next-size-adjust\",\"content\":\"\"}]]}]]}],false]],\"m\":\"$undefined\",\"G\":[\"$c\",[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/42d9c8ef7b6b1b3d.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/f4db1b58a3b2c635.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}],[\"$\",\"link\",\"2\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/a9e10e7d913f3b84.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]]],\"s\":false,\"S\":true}\n"])</script><script>self.__next_f.push([1,"e:I[33720,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8450\",\"static/chunks/app/%5Blocale%5D/layout-376dd1dc37535351.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"Analytics\"]\nf:I[5595,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8450\",\"static/chunks/app/%5Blocale%5D/layout-376dd1dc37535351.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"SpeedInsights\"]\n4:[\"$\",\"html\",null,{\"className\":\"__variable_7eb81d __variable_9790f2\",\"dir\":\"ltr\",\"lang\":\"en-GB\",\"suppressHydrationWarning\":true,\"children\":[\"$\",\"body\",null,{\"suppressHydrationWarning\":true,\"children\":[\"$Ld\",[\"$\",\"a\",null,{\"rel\":\"me\",\"aria-hidden\":\"true\",\"className\":\"hidden\",\"href\":\"https://social.lfx.dev/@nodejs\"}],[[\"$\",\"$Le\",null,{}],[\"$\",\"$Lf\",null,{}]]]}]}]\n"])</script><script>self.__next_f.push([1,"10:I[63054,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n11:I[27270,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8450\",\"static/chunks/app/%5Blocale%5D/layout-376dd1dc37535351.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"ThemeProvider\"]\n12:I[4576,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8450\",\"static/chunks/app/%5Blocale%5D/layout-376dd1dc37535351.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n13:I[60038,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-538"])</script><script>self.__next_f.push([1,"17b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3891\",\"static/chunks/3891-49a835c05af2af63.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8530\",\"static/chunks/app/%5Blocale%5D/error-1ae7101ee3725219.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n14:I[83952,[\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3891\",\"static/chunks/3891-49a835c05af2af63.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3224\",\"static/chunks/app/%5Blocale%5D/not-found-4ef8580001301355.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n"])</script><script>self.__next_f.push([1,"d:[\"$\",\"$L10\",null,{\"locale\":\"en\",\"now\":\"$D2025-02-21T15:05:58.367Z\",\"timeZone\":\"Etc/UTC\",\"messages\":{\"components\":{\"containers\":{\"footer\":{\"links\":{\"trademarkPolicy\":\"Trademark Policy\",\"privacyPolicy\":\"Privacy Policy\",\"versionSupport\":\"Version Support\",\"codeOfConduct\":\"Code of Conduct\",\"security\":\"Security Policy\"}},\"navBar\":{\"controls\":{\"toggle\":\"Toggle navigation menu\"},\"links\":{\"about\":\"About\",\"download\":\"Download\",\"docs\":\"Docs\",\"guides\":\"Guides\",\"learn\":\"Learn\",\"security\":\"Security\",\"certification\":\"Certification\",\"blog\":\"Blog\",\"contribute\":\"Contribute\"}}},\"navigation\":{\"learn\":{\"gettingStarted\":{\"links\":{\"gettingStarted\":\"Getting Started\",\"introductionToNodejs\":\"Introduction to Node.js\",\"howMuchJavascriptDoYouNeedToKnowToUseNodejs\":\"How much JavaScript do you need to know to use Node.js?\",\"differencesBetweenNodejsAndTheBrowser\":\"Differences between Node.js and the Browser\",\"theV8JavascriptEngine\":\"The V8 JavaScript Engine\",\"anIntroductionToTheNpmPackageManager\":\"An introduction to the npm package manager\",\"ecmascript2015Es6AndBeyond\":\"ECMAScript 2015 (ES6) and beyond\",\"nodejsTheDifferenceBetweenDevelopmentAndProduction\":\"Node.js, the difference between development and production\",\"nodejsWithWebassembly\":\"Node.js with WebAssembly\",\"debugging\":\"Debugging Node.js\",\"profiling\":\"Profiling Node.js Applications\",\"securityBestPractices\":\"Security Best Practices\"}},\"typescript\":{\"links\":{\"typescript\":\"TypeScript\",\"introduction\":\"Introduction to TypeScript\",\"transpile\":\"Running TypeScript code using transpilation\",\"run\":\"Running TypeScript with a runner\",\"runNatively\":\"Running TypeScript Natively\",\"publishingTSPackage\":\"Publishing a TypeScript package\"}},\"asynchronousWork\":{\"links\":{\"asynchronousWork\":\"Asynchronous Work\",\"asynchronousFlowControl\":\"Asynchronous flow control\",\"overviewOfBlockingVsNonBlocking\":\"Overview of Blocking vs Non-Blocking\",\"javascriptAsynchronousProgrammingAndCallbacks\":\"JavaScript Asynchronous Programming and Callbacks\",\"discoverJavascriptTimers\":\"Discover JavaScript Timers\",\"eventLoopTimersAndNexttick\":\"The Node.js Event Loop\",\"theNodejsEventEmitter\":\"The Node.js Event Emitter\",\"understandingProcessnexttick\":\"Understanding process.nextTick()\",\"understandingSetimmediate\":\"Understanding setImmediate()\",\"dontBlockTheEventLoop\":\"Don't Block the Event Loop\"}},\"manipulatingFiles\":{\"links\":{\"manipulatingFiles\":\"Manipulating Files\",\"nodejsFileStats\":\"Node.js file stats\",\"nodejsFilePaths\":\"Node.js File Paths\",\"workingWithFileDescriptorsInNodejs\":\"Working with file descriptors in Node.js\",\"readingFilesWithNodejs\":\"Reading files with Node.js\",\"writingFilesWithNodejs\":\"Writing files with Node.js\",\"workingWithFoldersInNodejs\":\"Working with folders in Node.js\",\"workingWithDifferentFilesystems\":\"How to work with Different Filesystems\"}},\"commandLine\":{\"links\":{\"commandLine\":\"Command Line\",\"runNodejsScriptsFromTheCommandLine\":\"Run Node.js scripts from the command line\",\"howToReadEnvironmentVariablesFromNodejs\":\"How to read environment variables from Node.js\",\"howToUseTheNodejsRepl\":\"How to use the Node.js REPL\",\"outputToTheCommandLineUsingNodejs\":\"Output to the command line using Node.js\",\"acceptInputFromTheCommandLineInNodejs\":\"Accept input from the command line in Node.js\"}},\"modules\":{\"links\":{\"modules\":\"Modules\",\"publishingAPackage\":\"Publishing a package\",\"publishingNodeApiModules\":\"How to publish a Node-API package\",\"anatomyOfAnHttpTransaction\":\"Anatomy of an HTTP Transaction\",\"abiStability\":\"ABI Stability\",\"howToUseStreams\":\"How to use streams\",\"backpressuringInStreams\":\"Backpressuring in Streams\"}},\"diagnostics\":{\"links\":{\"diagnostics\":\"Diagnostics\",\"userJourney\":\"User Journey\",\"memory\":\"Memory\",\"liveDebugging\":\"Live Debugging\",\"poorPerformance\":\"Poor Performance\",\"flameGraphs\":\"Flame Graphs\"}},\"testRunner\":{\"links\":{\"testRunner\":\"Test Runner\",\"introduction\":\"Discovering Node.js's test runner\",\"usingTestRunner\":\"Using Node.js's test runner\",\"mocking\":\"Mocking in tests\"}}},\"about\":{\"links\":{\"about\":\"About Node.js\",\"aboutSide\":\"About Node.js®\",\"branding\":\"Branding of Node.js\",\"governance\":\"Project Governance\",\"releases\":\"Node.js Releases\",\"security\":\"Security Reporting\"}},\"getInvolved\":{\"links\":{\"getInvolved\":\"Get Involved\",\"collabSummit\":\"Collaboration Summit\",\"upcomingEvents\":\"Upcoming Events\",\"contribute\":\"Contribute to Node.js\",\"codeOfConduct\":\"Code of Conduct\"}}},\"downloadReleasesTable\":{\"version\":\"Node.js\",\"nApiVersion\":\"N-API\",\"npmVersion\":\"npm\",\"codename\":\"Codename\",\"releaseDate\":\"Released at\",\"actions\":{\"changelog\":\"Changelog\",\"releases\":\"Releases\",\"docs\":\"Docs\"}},\"pagination\":{\"next\":\"Next\",\"previous\":\"Previous\"},\"common\":{\"alertBox\":{\"info\":\"Info\",\"warning\":\"Warning\",\"danger\":\"Danger\"},\"breadcrumbs\":{\"navigateToHome\":\"Navigate to Home\"},\"crossLink\":{\"previous\":\"Prev\",\"next\":\"Next\"},\"codebox\":{\"copy\":\"Copy to clipboard\",\"copied\":\"Copied to clipboard!\"},\"pagination\":{\"prev\":\"Previous\",\"prevAriaLabel\":\"Previous page\",\"next\":\"Next\",\"nextAriaLabel\":\"Next page\",\"defaultLabel\":\"Pagination\",\"pageLabel\":\"Go to page {pageNumber}\"},\"sidebar\":{\"title\":\"Change page\"},\"languageDropdown\":{\"label\":\"Choose Language\"},\"themeToggle\":{\"label\":\"Toggle Dark Mode\"}},\"metabar\":{\"lastUpdated\":\"Last Updated\",\"readingTime\":\"Reading Time\",\"addedIn\":\"Added In\",\"author\":\"Author\",\"authors\":\"Authors\",\"contribute\":\"Contribute\",\"contributeText\":\"Edit this page\",\"viewAs\":\"View as\",\"tableOfContents\":\"Table of Contents\"},\"search\":{\"searchBox\":{\"placeholder\":\"Start typing...\"}},\"blog\":{\"blogHeader\":{\"subtitle\":\"The latest Node.js news, case studies, tutorials, and resources.\",\"rssLink\":\"RSS feed\"}}},\"layouts\":{\"blogPost\":{\"author\":{\"byLine\":\"{author, select, null {} other {By {author}, }}\"}},\"blogIndex\":{\"categoryName\":\"{category, select, all {Blog} other {{category} Blog Posts}}\"},\"blog\":{\"title\":\"Blog\",\"selectCategory\":\"Categories\",\"categories\":{\"all\":\"Everything\",\"announcements\":\"Announcements\",\"release\":\"Releases\",\"vulnerability\":\"Vulnerabilities\",\"advisory-board\":\"Advisory Board\",\"community\":\"Community\",\"feature\":\"Feature\",\"module\":\"Module\",\"npm\":\"npm\",\"uncategorized\":\"Uncategorized\",\"video\":\"Video\",\"weekly\":\"Weekly Updates\",\"wg\":\"Working Groups\",\"events\":\"Events\"}},\"error\":{\"notFound\":{\"title\":\"Page could not be found\",\"description\":\"Sorry, we couldn't find the page you're after! Try starting again from the homepage.\"},\"internalServerError\":{\"title\":\"Internal Server Error\",\"description\":\"This page has thrown a non-recoverable error.\"},\"backToHome\":\"Back to Home\"},\"download\":{\"buttons\":{\"installer\":\"{os} Installer (.{extension})\",\"binary\":\"Standalone Binary (.{extension})\"},\"dropdown\":{\"platform\":\"Platform\",\"os\":\"Operating System\",\"version\":\"Version\",\"installMethod\":\"Install Method\",\"packageManager\":\"Package Manager\",\"unknown\":\"Unknown\",\"platformGroups\":{\"official\":\"Recommended (Official)\",\"unofficial\":\"Community (Unofficial)\"}},\"codeBox\":{\"unsupportedVersionWarning\":\"This version is out of maintenance. Please use a currently supported version. \u003clink\u003eUnderstand EOL support.\u003c/link\u003e\",\"communityPlatformInfo\":\"Installation methods that involve community software are supported by the teams maintaining that software.\",\"externalSupportInfo\":\"If you encounter any issues please visit \u003clink\u003e{platform}'s website\u003c/link\u003e\",\"noScriptDetected\":\"This page requires JavaScript. You can download Node.js without JavaScript by visiting the \u003clink\u003ereleases page\u003c/link\u003e directly.\",\"platformInfo\":{\"default\":\"{platform} and their installation scripts are not maintained by the Node.js project.\",\"nvm\":\"\\\"nvm\\\" is a cross-platform Node.js version manager.\",\"fnm\":\"\\\"fnm\\\" is a cross-platform Node.js version manager.\",\"brew\":\"Homebrew is a package manager for macOS and Linux.\",\"choco\":\"Chocolatey is a package manager for Windows.\",\"devbox\":\"Devbox creates isolated, reproducible development environments.\",\"docker\":\"Docker is a containerization platform.\",\"volta\":\"\\\"Volta\\\" is a cross-platform Node.js version manager.\"}}},\"logo\":\"Node.js logo\"}},\"children\":[\"$\",\"$L11\",null,{\"children\":[\"$\",\"$L12\",null,{\"children\":[\"$\",\"$L2\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$0:f:0:1:2:children:0\",\"children\"],\"error\":\"$13\",\"errorStyles\":[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/a9e10e7d913f3b84.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]],\"errorScripts\":[],\"template\":[\"$\",\"$L3\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/a9e10e7d913f3b84.css?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]],[\"$\",\"$L14\",null,{}]],\"forbidden\":\"$undefined\",\"unauthorized\":\"$undefined\"}]}]}]}]\n"])</script><script>self.__next_f.push([1,"b:[[\"$\",\"meta\",\"0\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1, maximum-scale=2\"}]]\n"])</script><script>self.__next_f.push([1,"15:I[11708,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"MatterProvider\"]\n16:I[39416,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"st"])</script><script>self.__next_f.push([1,"atic/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n17:I[41082,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n1b:I[99379,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"st"])</script><script>self.__next_f.push([1,"atic/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n27:I[78420,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.j"])</script><script>self.__next_f.push([1,"s?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"Root\"]\n28:I[78420,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"List\"]\n29:I[78420,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608"])</script><script>self.__next_f.push([1,"ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"Trigger\"]\n2a:I[78420,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\""])</script><script>self.__next_f.push([1,",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"Content\"]\n32:I[94150,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n33:I[87430,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbn"])</script><script>self.__next_f.push([1,"AfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n34:I[14719,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf"])</script><script>self.__next_f.push([1,"85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n35:T4b0,M10 1.25C5.16562 1.25 1.25 5.16562 1.25 10C1.25 13.8719 3.75469 17.1422 7.23281 18.3016C7.67031 18.3781 7.83437 18.1156 7.83437 17.8859C7.83437 17.6781 7.82344 16.9891 7.82344 16.2563C5.625 16.6609 5.05625 15.7203 4.88125 15.2281C4.78281 14.9766 4.35625 14.2 3.98438 13.9922C3.67812 13.8281 3.24063 13.4234 3.97344 13.4125C4.6625 13.4016 5.15469 14.0469 5.31875 14.3094C6.10625 15.6328 7.36406 15.2609 7.86719 15.0312C7.94375 14.4625 8.17344 14.0797 8.425 13.8609C6.47813 13.6422 4.44375 12.8875 4.44375 9.54062C4.44375 8.58906 4.78281 7.80156 5.34062 7.18906C5.25313 6.97031 4.94687 6.07344 5.42812 4.87031C5.42812 4.87031 6.16094 4.64063 7.83437 5.76719C8.53438 5.57031 9.27813 5.47187 10.0219 5.47187C10.7656 5.47187 11.5094 5.57031 12.2094 5.76719C13.8828 4.62969 14.6156 4.87031 14.6156 4.87031C15.0969 6.07344 14.7906 6.97031 14.7031 7.18906C15.2609 7.80156 15.6 8.57812 15.6 9.54062C15.6 12.8984 13.5547 13.6422 11.6078 13.8609C11.925 14.1344 12.1984 14.6594 12.1984 15.4797C12.1984 16.65 12.1875 17.5906 12.1875 17.8859C12.1875 18.1156 12.3516 18.3891 12.7891 18.3016C14.5261 17.7152 16.0355 16.5988 17.1048 15.1096C18.1741 13.6204 18.7495 11.8333 18.75 10C18.75 5.16562 14.8344 1.25 10 1.25Z36:T5d9,M73.8393 17.4898C72.6973 9.00165 65.2994 2.31235 56.5296 1.01614C55.05 0.797115 49.4441 0 36.4582 0H36.3612C23.3717 0 20.585 0.797115 19.1054 1.01614C10.5798 2.27644 2.79399 8.28712 0.904997 16.8758C-0.00358524 21.1056 -0.100549 25.7949 0.0682394 30.0965C0.308852 36.2651 0.355538 42.423 0.91577 48.5665C1.30307 52.6474 1.97872 56.6957 2.93763 60.6812C4.73325 68.042 12.0019 74.1676 19.1233 76.6666C26.7478 79.2728 34.9474 79.7055 42.8039 77.9162C43.6682 77.7151 44.5217 77.4817 45.3645 77.216C47.275 76.6092 49.5123 75.9305 51.1571 74.7385C51.1797 74.7217 51.1982 74.7001 51.2112 "])</script><script>self.__next_f.push([1,"74.6753C51.2243 74.6504 51.2316 74.6229 51.2325 74.5948V68.6416C51.2321 68.6154 51.2259 68.5896 51.2142 68.5661C51.2025 68.5426 51.1858 68.522 51.1651 68.5058C51.1444 68.4896 51.1204 68.4783 51.0948 68.4726C51.0692 68.4669 51.0426 68.467 51.0171 68.4729C45.9835 69.675 40.8254 70.2777 35.6502 70.2682C26.7439 70.2682 24.3486 66.042 23.6626 64.2826C23.1113 62.762 22.7612 61.1759 22.6212 59.5646C22.6197 59.5375 22.6247 59.5105 22.6357 59.4857C22.6466 59.4609 22.6633 59.4391 22.6843 59.422C22.7053 59.4048 22.73 59.3929 22.7565 59.3871C22.783 59.3813 22.8104 59.3818 22.8367 59.3886C27.7864 60.5826 32.8604 61.1853 37.9522 61.1839C39.1768 61.1839 40.3978 61.1839 41.6224 61.1516C46.7435 61.008 52.1411 60.7459 57.1796 59.7621C57.3053 59.7369 57.431 59.7154 57.5387 59.6831C65.4861 58.157 73.0493 53.3672 73.8178 41.2381C73.8465 40.7606 73.9184 36.2364 73.9184 35.7409C73.9219 34.0569 74.4606 23.7949 73.8393 17.4898Z"])</script><script>self.__next_f.push([1,"5:[\"$\",\"$L15\",null,{\"frontmatter\":{\"title\":\"The Node.js Event Loop\",\"layout\":\"learn\"},\"headings\":[{\"depth\":1,\"value\":\"The Node.js Event Loop\",\"data\":{\"id\":\"the-nodejs-event-loop\"}},{\"depth\":2,\"value\":\"What is the Event Loop?\",\"data\":{\"id\":\"what-is-the-event-loop\"}},{\"depth\":2,\"value\":\"Event Loop Explained\",\"data\":{\"id\":\"event-loop-explained\"}},{\"depth\":2,\"value\":\"Phases Overview\",\"data\":{\"id\":\"phases-overview\"}},{\"depth\":2,\"value\":\"Phases in Detail\",\"data\":{\"id\":\"phases-in-detail\"}},{\"depth\":3,\"value\":\"timers\",\"data\":{\"id\":\"timers\"}},{\"depth\":3,\"value\":\"pending callbacks\",\"data\":{\"id\":\"pending-callbacks\"}},{\"depth\":3,\"value\":\"poll\",\"data\":{\"id\":\"poll\"}},{\"depth\":3,\"value\":\"check\",\"data\":{\"id\":\"check\"}},{\"depth\":3,\"value\":\"close callbacks\",\"data\":{\"id\":\"close-callbacks\"}},{\"depth\":2,\"value\":\"setImmediate() vs setTimeout()\",\"data\":{\"id\":\"setimmediate-vs-settimeout\"}},{\"depth\":2,\"value\":\"process.nextTick()\",\"data\":{\"id\":\"processnexttick\"}},{\"depth\":3,\"value\":\"Understanding process.nextTick()\",\"data\":{\"id\":\"understanding-processnexttick\"}},{\"depth\":3,\"value\":\"Why would that be allowed?\",\"data\":{\"id\":\"why-would-that-be-allowed\"}},{\"depth\":2,\"value\":\"process.nextTick() vs setImmediate()\",\"data\":{\"id\":\"processnexttick-vs-setimmediate\"}},{\"depth\":2,\"value\":\"Why use process.nextTick()?\",\"data\":{\"id\":\"why-use-processnexttick\"}}],\"pathname\":\"/learn/asynchronous-work/event-loop-timers-and-nexttick\",\"readingTime\":{\"text\":\"13 min read\",\"minutes\":12.005,\"time\":720300,\"words\":2401},\"filename\":\"learn/asynchronous-work/event-loop-timers-and-nexttick.md\",\"children\":[[\"$\",\"$L16\",null,{}],[\"$\",\"div\",null,{\"className\":\"layouts_articleLayout__yakmC\",\"children\":[[\"$\",\"$L17\",null,{\"groups\":[{\"groupName\":\"Getting Started\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Introduction to Node.js\",\"link\":\"/learn/getting-started/introduction-to-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"How much JavaScript do you need to know to use Node.js?\",\"link\":\"/learn/getting-started/how-much-javascript-do-you-need-to-know-to-use-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Differences between Node.js and the Browser\",\"link\":\"/learn/getting-started/differences-between-nodejs-and-the-browser\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"The V8 JavaScript Engine\",\"link\":\"/learn/getting-started/the-v8-javascript-engine\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"An introduction to the npm package manager\",\"link\":\"/learn/getting-started/an-introduction-to-the-npm-package-manager\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"ECMAScript 2015 (ES6) and beyond\",\"link\":\"/learn/getting-started/ecmascript-2015-es6-and-beyond\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Node.js, the difference between development and production\",\"link\":\"/learn/getting-started/nodejs-the-difference-between-development-and-production\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Node.js with WebAssembly\",\"link\":\"/learn/getting-started/nodejs-with-webassembly\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Debugging Node.js\",\"link\":\"/learn/getting-started/debugging\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Profiling Node.js Applications\",\"link\":\"/learn/getting-started/profiling\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Security Best Practices\",\"link\":\"/learn/getting-started/security-best-practices\",\"items\":[]}]},{\"groupName\":\"TypeScript\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Introduction to TypeScript\",\"link\":\"/learn/typescript/introduction\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Running TypeScript code using transpilation\",\"link\":\"/learn/typescript/transpile\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Running TypeScript with a runner\",\"link\":\"/learn/typescript/run\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Running TypeScript Natively\",\"link\":\"/learn/typescript/run-natively\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Publishing a TypeScript package\",\"link\":\"/learn/typescript/publishing-a-ts-package\",\"items\":[]}]},{\"groupName\":\"Asynchronous Work\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Asynchronous flow control\",\"link\":\"/learn/asynchronous-work/asynchronous-flow-control\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Overview of Blocking vs Non-Blocking\",\"link\":\"/learn/asynchronous-work/overview-of-blocking-vs-non-blocking\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"JavaScript Asynchronous Programming and Callbacks\",\"link\":\"/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Discover JavaScript Timers\",\"link\":\"/learn/asynchronous-work/discover-javascript-timers\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"The Node.js Event Loop\",\"link\":\"/learn/asynchronous-work/event-loop-timers-and-nexttick\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"The Node.js Event Emitter\",\"link\":\"/learn/asynchronous-work/the-nodejs-event-emitter\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Understanding process.nextTick()\",\"link\":\"/learn/asynchronous-work/understanding-processnexttick\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Understanding setImmediate()\",\"link\":\"/learn/asynchronous-work/understanding-setimmediate\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Don't Block the Event Loop\",\"link\":\"/learn/asynchronous-work/dont-block-the-event-loop\",\"items\":[]}]},{\"groupName\":\"Manipulating Files\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Node.js file stats\",\"link\":\"/learn/manipulating-files/nodejs-file-stats\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Node.js File Paths\",\"link\":\"/learn/manipulating-files/nodejs-file-paths\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Working with file descriptors in Node.js\",\"link\":\"/learn/manipulating-files/working-with-file-descriptors-in-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Reading files with Node.js\",\"link\":\"/learn/manipulating-files/reading-files-with-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Writing files with Node.js\",\"link\":\"/learn/manipulating-files/writing-files-with-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Working with folders in Node.js\",\"link\":\"/learn/manipulating-files/working-with-folders-in-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"How to work with Different Filesystems\",\"link\":\"/learn/manipulating-files/working-with-different-filesystems\",\"items\":[]}]},{\"groupName\":\"Command Line\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Run Node.js scripts from the command line\",\"link\":\"/learn/command-line/run-nodejs-scripts-from-the-command-line\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"How to read environment variables from Node.js\",\"link\":\"/learn/command-line/how-to-read-environment-variables-from-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"How to use the Node.js REPL\",\"link\":\"/learn/command-line/how-to-use-the-nodejs-repl\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Output to the command line using Node.js\",\"link\":\"/learn/command-line/output-to-the-command-line-using-nodejs\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Accept input from the command line in Node.js\",\"link\":\"/learn/command-line/accept-input-from-the-command-line-in-nodejs\",\"items\":[]}]},{\"groupName\":\"Modules\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Publishing a package\",\"link\":\"/learn/modules/publishing-a-package\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"How to publish a Node-API package\",\"link\":\"/learn/modules/publishing-node-api-modules\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Anatomy of an HTTP Transaction\",\"link\":\"/learn/modules/anatomy-of-an-http-transaction\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"ABI Stability\",\"link\":\"/learn/modules/abi-stability\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"How to use streams\",\"link\":\"/learn/modules/how-to-use-streams\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Backpressuring in Streams\",\"link\":\"/learn/modules/backpressuring-in-streams\",\"items\":[]}]},{\"groupName\":\"Diagnostics\",\"items\":[{\"target\":\"$undefined\",\"label\":\"User Journey\",\"link\":\"/learn/diagnostics/user-journey\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Memory\",\"link\":\"/learn/diagnostics/memory\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Live Debugging\",\"link\":\"/learn/diagnostics/live-debugging\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Poor Performance\",\"link\":\"/learn/diagnostics/poor-performance\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Flame Graphs\",\"link\":\"/learn/diagnostics/flame-graphs\",\"items\":[]}]},{\"groupName\":\"Test Runner\",\"items\":[{\"target\":\"$undefined\",\"label\":\"Discovering Node.js's test runner\",\"link\":\"/learn/test-runner/introduction\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Using Node.js's test runner\",\"link\":\"/learn/test-runner/using-test-runner\",\"items\":[]},{\"target\":\"$undefined\",\"label\":\"Mocking in tests\",\"link\":\"/learn/test-runner/mocking\",\"items\":[]}]}]}],[\"$\",\"div\",null,{\"children\":[[\"$\",\"main\",null,{\"children\":[[[\"$\",\"h1\",null,{\"id\":\"the-nodejs-event-loop\",\"children\":\"$L18\"}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"what-is-the-event-loop\",\"children\":\"$L19\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"The event loop is what allows Node.js to perform non-blocking I/O\\noperations — despite the fact that a single JavaScript thread is used by default — by\\noffloading operations to the system kernel whenever possible.\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Since most modern kernels are multi-threaded, they can handle multiple\\noperations executing in the background. When one of these operations\\ncompletes, the kernel tells Node.js so that the appropriate callback\\nmay be added to the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" queue to eventually be executed. We'll explain\\nthis in further detail later in this topic.\"]}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"event-loop-explained\",\"children\":\"$L1a\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"When Node.js starts, it initializes the event loop, processes the\\nprovided input script (or drops into the \",[\"$\",\"a\",null,{\"href\":\"https://nodejs.org/api/repl.html#repl_repl\",\"children\":\"REPL\"}],\", which is not covered in\\nthis document) which may make async API calls, schedule timers, or call\\n\",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\", then begins processing the event loop.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"The following diagram shows a simplified overview of the event loop's\\norder of operations.\"}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"\",\"showCopyButton\":\"$undefined\",\"className\":\"$undefined\",\"children\":[\"$\",\"code\",null,{\"children\":\" ┌───────────────────────────┐\\n┌─\u003e│ timers │\\n│ └─────────────┬─────────────┘\\n│ ┌─────────────┴─────────────┐\\n│ │ pending callbacks │\\n│ └─────────────┬─────────────┘\\n│ ┌─────────────┴─────────────┐\\n│ │ idle, prepare │\\n│ └─────────────┬─────────────┘ ┌───────────────┐\\n│ ┌─────────────┴─────────────┐ │ incoming: │\\n│ │ poll │\u003c─────┤ connections, │\\n│ └─────────────┬─────────────┘ │ data, etc. │\\n│ ┌─────────────┴─────────────┐ └───────────────┘\\n│ │ check │\\n│ └─────────────┬─────────────┘\\n│ ┌─────────────┴─────────────┐\\n└──┤ close callbacks │\\n └───────────────────────────┘\\n\"}]}],\"\\n\",[\"$\",\"blockquote\",null,{\"className\":\"Blockquote_wrapper__4GnmJ\",\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":\"Each box will be referred to as a \\\"phase\\\" of the event loop.\"}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"Each phase has a FIFO queue of callbacks to execute. While each phase is\\nspecial in its own way, generally, when the event loop enters a given\\nphase, it will perform any operations specific to that phase, then\\nexecute callbacks in that phase's queue until the queue has been\\nexhausted or the maximum number of callbacks has executed. When the\\nqueue has been exhausted or the callback limit is reached, the event\\nloop will move to the next phase, and so on.\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Since any of these operations may schedule \",[\"$\",\"em\",null,{\"children\":\"more\"}],\" operations and new\\nevents processed in the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase are queued by the kernel, poll\\nevents can be queued while polling events are being processed. As a\\nresult, long running callbacks can allow the poll phase to run much\\nlonger than a timer's threshold. See the \",\"$L1c\",\" and\\n\",\"$L1d\",\" sections for more details.\"]}],\"\\n\",[\"$\",\"blockquote\",null,{\"className\":\"Blockquote_wrapper__4GnmJ\",\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":\"There is a slight discrepancy between the Windows and the\\nUnix/Linux implementation, but that's not important for this\\ndemonstration. The most important parts are here. There are actually\\nseven or eight steps, but the ones we care about — ones that Node.js\\nactually uses - are those above.\"}],\"\\n\"]}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"phases-overview\",\"children\":\"$L1e\"}],\"\\n\",[\"$\",\"ul\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"strong\",null,{\"children\":\"timers\"}],\": this phase executes callbacks scheduled by \",[\"$\",\"code\",null,{\"children\":\"setTimeout()\"}],\"\\nand \",[\"$\",\"code\",null,{\"children\":\"setInterval()\"}],\".\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"strong\",null,{\"children\":\"pending callbacks\"}],\": executes I/O callbacks deferred to the next loop\\niteration.\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"strong\",null,{\"children\":\"idle, prepare\"}],\": only used internally.\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"strong\",null,{\"children\":\"poll\"}],\": retrieve new I/O events; execute I/O related callbacks (almost\\nall with the exception of close callbacks, the ones scheduled by timers,\\nand \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\"); node will block here when appropriate.\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"strong\",null,{\"children\":\"check\"}],\": \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" callbacks are invoked here.\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"strong\",null,{\"children\":\"close callbacks\"}],\": some close callbacks, e.g. \",[\"$\",\"code\",null,{\"children\":\"socket.on('close', ...)\"}],\".\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"Between each run of the event loop, Node.js checks if it is waiting for\\nany asynchronous I/O or timers and shuts down cleanly if there are not\\nany.\"}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"phases-in-detail\",\"children\":\"$L1f\"}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"timers\",\"children\":\"$L20\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"A timer specifies the \",[\"$\",\"strong\",null,{\"children\":\"threshold\"}],\" \",[\"$\",\"em\",null,{\"children\":\"after which\"}],\" a provided callback\\n\",[\"$\",\"em\",null,{\"children\":\"may be executed\"}],\" rather than the \",[\"$\",\"strong\",null,{\"children\":\"exact\"}],\" time a person \",[\"$\",\"em\",null,{\"children\":\"wants it to\\nbe executed\"}],\". Timers callbacks will run as early as they can be\\nscheduled after the specified amount of time has passed; however,\\nOperating System scheduling or the running of other callbacks may delay\\nthem.\"]}],\"\\n\",[\"$\",\"blockquote\",null,{\"className\":\"Blockquote_wrapper__4GnmJ\",\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Technically, the \",\"$L21\",\" controls when timers are executed.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"For example, say you schedule a timeout to execute after a 100 ms\\nthreshold, then your script starts asynchronously reading a file which\\ntakes 95 ms:\"}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" fs\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" require\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"node:fs\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"function\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" someAsyncOperation\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // Assume this takes 95ms to complete\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" fs\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"readFile\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"/path/to/file\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" timeoutScheduled\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" Date\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"now\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"setTimeout\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" delay\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" Date\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"now\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"() \"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"-\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" timeoutScheduled\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"`\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"$${\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"delay\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"ms have passed since I was scheduled\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"`\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"},\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\" 100\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\"// do someAsyncOperation which takes 95 ms to complete\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"someAsyncOperation\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" startCallback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" Date\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"now\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // do something that will take 10ms...\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" while\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\" (\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"Date\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"now\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"() \"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"-\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" startCallback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" \u003c\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\" 10\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\") \"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"{\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // do nothing\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" }\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"When the event loop enters the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase, it has an empty queue\\n(\",[\"$\",\"code\",null,{\"children\":\"fs.readFile()\"}],\" has not completed), so it will wait for the number of ms\\nremaining until the soonest timer's threshold is reached. While it is\\nwaiting 95 ms pass, \",[\"$\",\"code\",null,{\"children\":\"fs.readFile()\"}],\" finishes reading the file and its\\ncallback which takes 10 ms to complete is added to the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" queue and\\nexecuted. When the callback finishes, there are no more callbacks in the\\nqueue, so the event loop will see that the threshold of the soonest\\ntimer has been reached then wrap back to the \",[\"$\",\"strong\",null,{\"children\":\"timers\"}],\" phase to execute\\nthe timer's callback. In this example, you will see that the total delay\\nbetween the timer being scheduled and its callback being executed will\\nbe 105ms.\"]}],\"\\n\",[\"$\",\"blockquote\",null,{\"className\":\"Blockquote_wrapper__4GnmJ\",\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[\"To prevent the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase from starving the event loop, \",[\"$\",\"a\",null,{\"href\":\"https://libuv.org/\",\"children\":\"libuv\"}],\"\\n(the C library that implements the Node.js\\nevent loop and all of the asynchronous behaviors of the platform)\\nalso has a hard maximum (system dependent) before it stops polling for\\nmore events.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"pending-callbacks\",\"children\":\"$L22\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"This phase executes callbacks for some system operations such as types\\nof TCP errors. For example if a TCP socket receives \",[\"$\",\"code\",null,{\"children\":\"ECONNREFUSED\"}],\" when\\nattempting to connect, some *nix systems want to wait to report the\\nerror. This will be queued to execute in the \",[\"$\",\"strong\",null,{\"children\":\"pending callbacks\"}],\" phase.\"]}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"poll\",\"children\":\"$L23\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"The \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase has two main functions:\"]}],\"\\n\",[\"$\",\"ol\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":\"Calculating how long it should block and poll for I/O, then\"}],\"\\n\",[\"$\",\"li\",null,{\"children\":[\"Processing events in the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" queue.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"When the event loop enters the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase \",[\"$\",\"em\",null,{\"children\":\"and there are no timers\\nscheduled\"}],\", one of two things will happen:\"]}],\"\\n\",[\"$\",\"ul\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[[\"$\",\"em\",null,{\"children\":[\"If the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" queue \",[\"$\",\"strong\",null,{\"children\":\"is not empty\"}]]}],\", the event loop will iterate\\nthrough its queue of callbacks executing them synchronously until\\neither the queue has been exhausted, or the system-dependent hard limit\\nis reached.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[[\"$\",\"em\",null,{\"children\":[\"If the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" queue \",[\"$\",\"strong\",null,{\"children\":\"is empty\"}]]}],\", one of two more things will\\nhappen:\"]}],\"\\n\",[\"$\",\"ul\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[\"If scripts have been scheduled by \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\", the event loop\\nwill end the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase and continue to the \",[\"$\",\"strong\",null,{\"children\":\"check\"}],\" phase to\\nexecute those scheduled scripts.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[\"If scripts \",[\"$\",\"strong\",null,{\"children\":\"have not\"}],\" been scheduled by \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\", the\\nevent loop will wait for callbacks to be added to the queue, then\\nexecute them immediately.\"]}],\"\\n\"]}],\"\\n\"]}],\"\\n\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Once the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" queue is empty the event loop will check for timers\\n\",[\"$\",\"em\",null,{\"children\":\"whose time thresholds have been reached\"}],\". If one or more timers are\\nready, the event loop will wrap back to the \",[\"$\",\"strong\",null,{\"children\":\"timers\"}],\" phase to execute\\nthose timers' callbacks.\"]}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"check\",\"children\":\"$L24\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"This phase allows the event loop to execute callbacks immediately after the\\n\",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase has completed. If the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase becomes idle and\\nscripts have been queued with \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\", the event loop may\\ncontinue to the \",[\"$\",\"strong\",null,{\"children\":\"check\"}],\" phase rather than waiting.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" is actually a special timer that runs in a separate\\nphase of the event loop. It uses a libuv API that schedules callbacks to\\nexecute after the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase has completed.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Generally, as the code is executed, the event loop will eventually hit\\nthe \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase where it will wait for an incoming connection, request,\\netc. However, if a callback has been scheduled with \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\"\\nand the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase becomes idle, it will end and continue to the\\n\",[\"$\",\"strong\",null,{\"children\":\"check\"}],\" phase rather than waiting for \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" events.\"]}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"close-callbacks\",\"children\":\"$L25\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"If a socket or handle is closed abruptly (e.g. \",[\"$\",\"code\",null,{\"children\":\"socket.destroy()\"}],\"), the\\n\",[\"$\",\"code\",null,{\"children\":\"'close'\"}],\" event will be emitted in this phase. Otherwise it will be\\nemitted via \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\".\"]}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"setimmediate-vs-settimeout\",\"children\":\"$L26\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" and \",[\"$\",\"code\",null,{\"children\":\"setTimeout()\"}],\" are similar, but behave in different\\nways depending on when they are called.\"]}],\"\\n\",[\"$\",\"ul\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" is designed to execute a script once the\\ncurrent \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase completes.\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"code\",null,{\"children\":\"setTimeout()\"}],\" schedules a script to be run after a minimum threshold\\nin ms has elapsed.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"The order in which the timers are executed will vary depending on the\\ncontext in which they are called. If both are called from within the\\nmain module, then timing will be bound by the performance of the process\\n(which can be impacted by other applications running on the machine).\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"For example, if we run the following script which is not within an I/O\\ncycle (i.e. the main module), the order in which the two timers are\\nexecuted is non-deterministic, as it is bound by the performance of the\\nprocess:\"}],\"\\n\",[\"$\",\"$L27\",null,{\"defaultValue\":\"js-0\",\"className\":\"Tabs_tabsRoot__iUO6B CodeTabs_root__nwOXE\",\"children\":[[\"$\",\"$L28\",null,{\"className\":\"Tabs_tabsList__7N46C\",\"children\":[[[\"$\",\"$L29\",\"js-0\",{\"value\":\"js-0\",\"className\":\"Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ\",\"children\":[\"JS\",null]}],[\"$\",\"$L29\",\"bash-1\",{\"value\":\"bash-1\",\"className\":\"Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ\",\"children\":[\"BASH\",null]}]],\"$undefined\"]}],[[\"$\",\"$L2a\",\"js-0\",{\"value\":\"js-0\",\"children\":[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\"// timeout_vs_immediate.js\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"setTimeout\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"timeout\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"},\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\" 0\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"setImmediate\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"immediate\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}]}],[\"$\",\"$L2a\",\"bash-1\",{\"value\":\"bash-1\",\"children\":[\"$\",\"$L1b\",null,{\"language\":\"Shell\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-bash\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"$$\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" node\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" timeout_vs_immediate.js\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"timeout\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"immediate\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"$$\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" node\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" timeout_vs_immediate.js\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"immediate\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"timeout\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}]}]]]}],[\"$\",\"p\",null,{\"children\":\"However, if you move the two calls within an I/O cycle, the immediate\\ncallback is always executed first:\"}],\"\\n\",[\"$\",\"$L27\",null,{\"defaultValue\":\"js-0\",\"className\":\"Tabs_tabsRoot__iUO6B CodeTabs_root__nwOXE\",\"children\":[[\"$\",\"$L28\",null,{\"className\":\"Tabs_tabsList__7N46C\",\"children\":[[[\"$\",\"$L29\",\"js-0\",{\"value\":\"js-0\",\"className\":\"Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ\",\"children\":[\"JS\",null]}],[\"$\",\"$L29\",\"bash-1\",{\"value\":\"bash-1\",\"className\":\"Tabs_tabsTrigger__Gkq_a CodeTabs_trigger__n7KrQ\",\"children\":[\"BASH\",null]}]],\"$undefined\"]}],[[\"$\",\"$L2a\",\"js-0\",{\"value\":\"js-0\",\"children\":[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\"// timeout_vs_immediate.js\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" fs\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" require\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"node:fs\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"fs\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"readFile\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"__filename\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" ()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" setTimeout\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"timeout\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" },\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\" 0\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" setImmediate\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"immediate\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" }\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}]}],[\"$\",\"$L2a\",\"bash-1\",{\"value\":\"bash-1\",\"children\":[\"$\",\"$L1b\",null,{\"language\":\"Shell\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-bash\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"$$\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" node\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" timeout_vs_immediate.js\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"immediate\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"timeout\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"$$\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" node\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\" timeout_vs_immediate.js\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"immediate\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"timeout\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}]}]]]}],[\"$\",\"p\",null,{\"children\":[\"The main advantage to using \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" over \",[\"$\",\"code\",null,{\"children\":\"setTimeout()\"}],\" is\\n\",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" will always be executed before any timers if scheduled\\nwithin an I/O cycle, independently of how many timers are present.\"]}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"processnexttick\",\"children\":\"$L2b\"}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"understanding-processnexttick\",\"children\":\"$L2c\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"You may have noticed that \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" was not displayed in the\\ndiagram, even though it's a part of the asynchronous API. This is because\\n\",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" is not technically part of the event loop. Instead,\\nthe \",[\"$\",\"code\",null,{\"children\":\"nextTickQueue\"}],\" will be processed after the current operation is\\ncompleted, regardless of the current phase of the event loop. Here,\\nan \",[\"$\",\"em\",null,{\"children\":\"operation\"}],\" is defined as a transition from the\\nunderlying C/C++ handler, and handling the JavaScript that needs to be\\nexecuted.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Looking back at our diagram, any time you call \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" in a\\ngiven phase, all callbacks passed to \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" will be\\nresolved before the event loop continues. This can create some bad\\nsituations because \",[\"$\",\"strong\",null,{\"children\":[\"it allows you to \\\"starve\\\" your I/O by making\\nrecursive \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" calls\"]}],\", which prevents the event loop\\nfrom reaching the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase.\"]}],\"\\n\",[\"$\",\"h3\",null,{\"id\":\"why-would-that-be-allowed\",\"children\":\"$L2d\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"Why would something like this be included in Node.js? Part of it is a\\ndesign philosophy where an API should always be asynchronous even where\\nit doesn't have to be. Take this code snippet for example:\"}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"function\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" apiCall\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"arg\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" if\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\" (\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"typeof\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" arg\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" !==\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" '\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"string\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" return\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" process\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"nextTick\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" new\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" TypeError\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"argument should be string\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\" )\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"The snippet does an argument check and if it's not correct, it will pass\\nthe error to the callback. The API updated fairly recently to allow\\npassing arguments to \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" allowing it to take any\\narguments passed after the callback to be propagated as the arguments to\\nthe callback so you don't have to nest functions.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"What we're doing is passing an error back to the user but only \",[\"$\",\"em\",null,{\"children\":\"after\"}],\"\\nwe have allowed the rest of the user's code to execute. By using\\n\",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" we guarantee that \",[\"$\",\"code\",null,{\"children\":\"apiCall()\"}],\" always runs its\\ncallback \",[\"$\",\"em\",null,{\"children\":\"after\"}],\" the rest of the user's code and \",[\"$\",\"em\",null,{\"children\":\"before\"}],\" the event loop\\nis allowed to proceed. To achieve this, the JS call stack is allowed to\\nunwind then immediately execute the provided callback which allows a\\nperson to make recursive calls to \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" without reaching a\\n\",[\"$\",\"code\",null,{\"children\":\"RangeError: Maximum call stack size exceeded from v8\"}],\".\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"This philosophy can lead to some potentially problematic situations.\\nTake this snippet for example:\"}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"let\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\"// this has an asynchronous signature, but calls callback synchronously\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"function\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" someAsyncApiCall\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\"// the callback is called before `someAsyncApiCall` completes.\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"someAsyncApiCall\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // since someAsyncApiCall hasn't completed, bar hasn't been assigned any value\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // undefined\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\" 1\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"The user defines \",[\"$\",\"code\",null,{\"children\":\"someAsyncApiCall()\"}],\" to have an asynchronous signature,\\nbut it actually operates synchronously. When it is called, the callback\\nprovided to \",[\"$\",\"code\",null,{\"children\":\"someAsyncApiCall()\"}],\" is called in the same phase of the\\nevent loop because \",[\"$\",\"code\",null,{\"children\":\"someAsyncApiCall()\"}],\" doesn't actually do anything\\nasynchronously. As a result, the callback tries to reference \",[\"$\",\"code\",null,{\"children\":\"bar\"}],\" even\\nthough it may not have that variable in scope yet, because the script has not\\nbeen able to run to completion.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"By placing the callback in a \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\", the script still has the\\nability to run to completion, allowing all the variables, functions,\\netc., to be initialized prior to the callback being called. It also has\\nthe advantage of not allowing the event loop to continue. It may be\\nuseful for the user to be alerted to an error before the event loop is\\nallowed to continue. Here is the previous example using \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\":\"]}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"let\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"function\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" someAsyncApiCall\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" process\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"nextTick\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"callback\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"someAsyncApiCall\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // 1\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"bar\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\" 1\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"Here's another real world example:\"}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" server\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" net\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"createServer\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"listen\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\"8080\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"server\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"on\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"listening\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" ()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"When only a port is passed, the port is bound immediately. So, the\\n\",[\"$\",\"code\",null,{\"children\":\"'listening'\"}],\" callback could be called immediately. The problem is that the\\n\",[\"$\",\"code\",null,{\"children\":\".on('listening')\"}],\" callback will not have been set by that time.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"To get around this, the \",[\"$\",\"code\",null,{\"children\":\"'listening'\"}],\" event is queued in a \",[\"$\",\"code\",null,{\"children\":\"nextTick()\"}],\"\\nto allow the script to run to completion. This allows the user to set\\nany event handlers they want.\"]}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"processnexttick-vs-setimmediate\",\"children\":\"$L2e\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"We have two calls that are similar as far as users are concerned, but\\ntheir names are confusing.\"}],\"\\n\",[\"$\",\"ul\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" fires immediately on the same phase\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" fires on the following iteration or 'tick' of the\\nevent loop\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"In essence, the names should be swapped. \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" fires more\\nimmediately than \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\", but this is an artifact of the past\\nwhich is unlikely to change. Making this switch would break a large\\npercentage of the packages on npm. Every day more new modules are being\\nadded, which means every day we wait, more potential breakages occur.\\nWhile they are confusing, the names themselves won't change.\"]}],\"\\n\",[\"$\",\"blockquote\",null,{\"className\":\"Blockquote_wrapper__4GnmJ\",\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":[\"We recommend developers use \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" in all cases because it's\\neasier to reason about.\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"h2\",null,{\"id\":\"why-use-processnexttick\",\"children\":\"$L2f\"}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"There are two main reasons:\"}],\"\\n\",[\"$\",\"ol\",null,{\"children\":[\"\\n\",[\"$\",\"li\",null,{\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":\"Allow users to handle errors, cleanup any then unneeded resources, or\\nperhaps try the request again before the event loop continues.\"}],\"\\n\"]}],\"\\n\",[\"$\",\"li\",null,{\"children\":[\"\\n\",[\"$\",\"p\",null,{\"children\":\"At times it's necessary to allow a callback to run after the call\\nstack has unwound but before the event loop continues.\"}],\"\\n\"]}],\"\\n\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":\"One example is to match the user's expectations. Simple example:\"}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" server\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" net\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"createServer\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"server\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"on\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"connection\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" conn\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"server\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"listen\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#B48EAD\"},\"children\":\"8080\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"server\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"on\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"listening\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" ()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Say that \",[\"$\",\"code\",null,{\"children\":\"listen()\"}],\" is run at the beginning of the event loop, but the\\nlistening callback is placed in a \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\". Unless a\\nhostname is passed, binding to the port will happen immediately. For\\nthe event loop to proceed, it must hit the \",[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase, which means\\nthere is a non-zero chance that a connection could have been received\\nallowing the connection event to be fired before the listening event.\"]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"Another example is extending an \",[\"$\",\"code\",null,{\"children\":\"EventEmitter\"}],\" and emitting an\\nevent from within the constructor:\"]}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" EventEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" require\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"node:events\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"class\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#8FBCBB\"},\"children\":\" MyEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" extends\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#8FBCBB\",\"fontWeight\":\"bold\"},\"children\":\" EventEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" constructor\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" super\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" this\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"emit\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"event\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" }\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" myEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" new\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" MyEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"myEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"on\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"event\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" ()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"an event occurred!\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}],\"\\n\",[\"$\",\"p\",null,{\"children\":[\"You can't emit an event from the constructor immediately\\nbecause the script will not have processed to the point where the user\\nassigns a callback to that event. So, within the constructor itself,\\nyou can use \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" to set a callback to emit the event\\nafter the constructor has finished, which provides the expected results:\"]}],\"\\n\",[\"$\",\"$L1b\",null,{\"language\":\"JavaScript\",\"showCopyButton\":\"$undefined\",\"className\":\"shiki nord language-js\",\"children\":[\"$\",\"code\",null,{\"children\":[[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" EventEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" require\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"node:events\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"class\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#8FBCBB\"},\"children\":\" MyEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" extends\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#8FBCBB\",\"fontWeight\":\"bold\"},\"children\":\" EventEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" constructor\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" super\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#707e99\"},\"children\":\" // use nextTick to emit the event once a handler is assigned\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" process\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"nextTick\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" this\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"emit\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"event\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" }\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" }\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\"const\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" myEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" new\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\" MyEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\"myEmitter\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"on\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"event\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\",\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" ()\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\" =\u003e\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\" {\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9\"},\"children\":\" console\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\".\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#88C0D0\"},\"children\":\"log\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\"(\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#A3BE8C\"},\"children\":\"an event occurred!\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"'\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\",\"children\":[[\"$\",\"span\",null,{\"style\":{\"color\":\"#ECEFF4\"},\"children\":\"}\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#D8DEE9FF\"},\"children\":\")\"}],[\"$\",\"span\",null,{\"style\":{\"color\":\"#81A1C1\"},\"children\":\";\"}]]}],\"\\n\",[\"$\",\"span\",null,{\"className\":\"line\"}]]}]}]],[\"$\",\"div\",null,{\"className\":\"mt-4 grid w-full grid-cols-1 gap-4 md:grid-cols-2\",\"children\":[\"$L30\",\"$L31\"]}]]}],[\"$\",\"$L32\",null,{}]]}],[\"$\",\"$L33\",null,{\"navKeys\":[\"learn\"]}]]}],[\"$\",\"footer\",null,{\"className\":\"Footer_footer__7ZVsN\",\"children\":[[\"$\",\"div\",null,{\"className\":\"Footer_sectionPrimary__6nfYa\",\"children\":[[\"$\",\"$L34\",\"https://trademark-policy.openjsf.org/\",{\"href\":\"https://trademark-policy.openjsf.org/\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":\"Trademark Policy\"}],false]}],[\"$\",\"$L34\",\"https://privacy-policy.openjsf.org/\",{\"href\":\"https://privacy-policy.openjsf.org/\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":\"Privacy Policy\"}],false]}],[\"$\",\"$L34\",\"/about/previous-releases\",{\"href\":\"/about/previous-releases\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":true,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":\"Version Support\"}],false]}],[\"$\",\"$L34\",\"https://github.com/openjs-foundation/cross-project-council/blob/main/CODE_OF_CONDUCT.md\",{\"href\":\"https://github.com/openjs-foundation/cross-project-council/blob/main/CODE_OF_CONDUCT.md\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":\"Code of Conduct\"}],false]}],[\"$\",\"$L34\",\"https://github.com/nodejs/node/security/policy\",{\"href\":\"https://github.com/nodejs/node/security/policy\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":\"Security Policy\"}],false]}]]}],[\"$\",\"div\",null,{\"className\":\"Footer_sectionSecondary__e8pQF\",\"children\":[[\"$\",\"$L34\",null,{\"href\":\"https://openjsf.org/\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"© \",\"OpenJS Foundation\"]}],false]}],[\"$\",\"div\",null,{\"className\":\"Footer_social__fgBIS\",\"children\":[[\"$\",\"$L34\",\"github\",{\"href\":\"https://github.com/nodejs/node\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"$\",\"svg\",null,{\"width\":20,\"height\":20,\"viewBox\":\"0 0 20 20\",\"fill\":\"currentColor\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"aria-label\":\"https://github.com/nodejs/node\",\"children\":[\"$\",\"path\",null,{\"d\":\"$35\"}]}]}],false]}],[\"$\",\"$L34\",\"mastodon\",{\"href\":\"https://social.lfx.dev/@nodejs\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"$\",\"svg\",null,{\"width\":20,\"height\":20,\"viewBox\":\"0 0 75 79\",\"fill\":\"none\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"aria-label\":\"https://social.lfx.dev/@nodejs\",\"children\":[[\"$\",\"path\",null,{\"d\":\"$36\",\"fill\":\"url(#paint0_linear_549_34)\"}],[\"$\",\"path\",null,{\"d\":\"M61.2484 27.0263V48.114H52.8916V27.6475C52.8916 23.3388 51.096 21.1413 47.4437 21.1413C43.4287 21.1413 41.4177 23.7409 41.4177 28.8755V40.0782H33.1111V28.8755C33.1111 23.7409 31.0965 21.1413 27.0815 21.1413C23.4507 21.1413 21.6371 23.3388 21.6371 27.6475V48.114H13.2839V27.0263C13.2839 22.7176 14.384 19.2946 16.5843 16.7572C18.8539 14.2258 21.8311 12.926 25.5264 12.926C29.8036 12.926 33.0357 14.5705 35.1905 17.8559L37.2698 21.346L39.3527 17.8559C41.5074 14.5705 44.7395 12.926 49.0095 12.926C52.7013 12.926 55.6784 14.2258 57.9553 16.7572C60.1531 19.2922 61.2508 22.7152 61.2484 27.0263Z\",\"fill\":\"white\"}],[\"$\",\"defs\",null,{\"children\":[\"$\",\"linearGradient\",null,{\"id\":\"paint0_linear_549_34\",\"x1\":\"37.0692\",\"y1\":\"0\",\"x2\":\"37.0692\",\"y2\":\"79\",\"gradientUnits\":\"userSpaceOnUse\",\"children\":[[\"$\",\"stop\",null,{\"stopColor\":\"#6364FF\"}],[\"$\",\"stop\",null,{\"offset\":\"1\",\"stopColor\":\"#563ACC\"}]]}]}]]}]}],false]}],[\"$\",\"$L34\",\"twitter\",{\"href\":\"https://twitter.com/nodejs\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"$\",\"svg\",null,{\"width\":20,\"height\":20,\"viewBox\":\"0 0 24 24\",\"fill\":\"currentColor\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"aria-label\":\"https://twitter.com/nodejs\",\"children\":[\"$\",\"path\",null,{\"d\":\"M18.901 1.153h3.68l-8.04 9.19L24 22.846h-7.406l-5.8-7.584-6.638 7.584H.474l8.6-9.83L0 1.154h7.594l5.243 6.932ZM17.61 20.644h2.039L6.486 3.24H4.298Z\"}]}]}],false]}],[\"$\",\"$L34\",\"bluesky\",{\"href\":\"https://bsky.app/profile/nodejs.org\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"$\",\"svg\",null,{\"width\":20,\"height\":20,\"viewBox\":\"0 0 568 501\",\"fill\":\"none\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"aria-label\":\"https://bsky.app/profile/nodejs.org\",\"children\":[\"$\",\"path\",null,{\"fill\":\"#1185FE\",\"d\":\"M123.121 33.664C188.241 82.553 258.281 181.68 284 234.873c25.719-53.192 95.759-152.32 160.879-201.21C491.866-1.611 568-28.906 568 57.947c0 17.346-9.945 145.713-15.778 166.555-20.275 72.453-94.155 90.933-159.875 79.748C507.222 323.8 536.444 388.56 473.333 453.32c-119.86 122.992-172.272-30.859-185.702-70.281-2.462-7.227-3.614-10.608-3.631-7.733-.017-2.875-1.169.506-3.631 7.733-13.43 39.422-65.842 193.273-185.702 70.281-63.111-64.76-33.89-129.52 80.986-149.071-65.72 11.185-139.6-7.295-159.875-79.748C9.945 203.659 0 75.291 0 57.946 0-28.906 76.135-1.612 123.121 33.664Z\"}]}]}],false]}],[\"$\",\"$L34\",\"slack\",{\"href\":\"https://slack-invite.openjsf.org/\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"$\",\"svg\",null,{\"width\":20,\"height\":20,\"viewBox\":\"0 0 20 20\",\"fill\":\"none\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"aria-label\":\"https://slack-invite.openjsf.org/\",\"children\":[[\"$\",\"path\",null,{\"fill\":\"#2EB67D\",\"d\":\"M16.563 9.375A1.562 1.562 0 1 0 15 7.813v1.562h1.563Zm-4.375 0c.862 0 1.562-.7 1.562-1.563V3.438a1.563 1.563 0 0 0-3.125 0v4.374c0 .863.7 1.563 1.563 1.563Z\"}],[\"$\",\"path\",null,{\"fill\":\"#E01E5A\",\"d\":\"M3.437 10.625A1.562 1.562 0 1 0 5 12.187v-1.562H3.437Zm4.376 0c-.863 0-1.563.7-1.563 1.563v4.374a1.563 1.563 0 0 0 3.125 0v-4.374c0-.863-.7-1.563-1.563-1.563Z\"}],[\"$\",\"path\",null,{\"fill\":\"#ECB22E\",\"d\":\"M10.625 16.563A1.562 1.562 0 1 0 12.187 15h-1.562v1.563Zm0-4.375c0 .862.7 1.562 1.563 1.562h4.374a1.562 1.562 0 1 0 0-3.125h-4.374c-.863 0-1.563.7-1.563 1.563Z\"}],[\"$\",\"path\",null,{\"fill\":\"#36C5F0\",\"d\":\"M9.375 3.437A1.562 1.562 0 1 0 7.813 5h1.562V3.437Zm0 4.376c0-.863-.7-1.563-1.563-1.563H3.438a1.563 1.563 0 0 0 0 3.125h4.374c.863 0 1.563-.7 1.563-1.563Z\"}]]}]}],false]}],[\"$\",\"$L34\",\"linkedin\",{\"href\":\"https://www.linkedin.com/company/node-js\",\"className\":\"NavItem_navItem__0mE9T NavItem_footer__pMnwS\",\"activeClassName\":\"NavItem_active__P9lTF\",\"allowSubPath\":false,\"target\":\"$undefined\",\"children\":[[\"$\",\"span\",null,{\"className\":\"NavItem_label__9GW08\",\"children\":[\"$\",\"svg\",null,{\"xmlns\":\"http://www.w3.org/2000/svg\",\"viewBox\":\"0 0 24 24\",\"fill\":\"currentColor\",\"width\":20,\"height\":20,\"aria-label\":\"https://www.linkedin.com/company/node-js\",\"children\":[\"$\",\"path\",null,{\"d\":\"M20.5 2h-17A1.5 1.5 0 002 3.5v17A1.5 1.5 0 003.5 22h17a1.5 1.5 0 001.5-1.5v-17A1.5 1.5 0 0020.5 2zM8 19H5v-9h3zM6.5 8.25A1.75 1.75 0 118.3 6.5a1.78 1.78 0 01-1.8 1.75zM19 19h-3v-4.74c0-1.42-.6-1.93-1.38-1.93A1.74 1.74 0 0013 14.19a.66.66 0 000 .14V19h-3v-9h2.9v1.3a3.11 3.11 0 012.7-1.4c1.55 0 3.36.86 3.36 3.66z\"}]}]}],false]}]]}]]}]]}]]}]\n"])</script><script>self.__next_f.push([1,"9:[[\"$\",\"meta\",\"0\",{\"charSet\":\"utf-8\"}],[\"$\",\"title\",\"1\",{\"children\":\"Node.js — The Node.js Event Loop\"}],[\"$\",\"meta\",\"2\",{\"name\":\"description\",\"content\":\"Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.\"}],[\"$\",\"meta\",\"3\",{\"name\":\"robots\",\"content\":\"index, follow\"}],[\"$\",\"link\",\"4\",{\"rel\":\"canonical\",\"href\":\"https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"5\",{\"rel\":\"alternate\",\"hrefLang\":\"x-default\",\"href\":\"https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"6\",{\"rel\":\"alternate\",\"hrefLang\":\"en\",\"href\":\"https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"7\",{\"rel\":\"alternate\",\"hrefLang\":\"es\",\"href\":\"https://nodejs.org/es/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"8\",{\"rel\":\"alternate\",\"hrefLang\":\"fr\",\"href\":\"https://nodejs.org/fr/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"9\",{\"rel\":\"alternate\",\"hrefLang\":\"id\",\"href\":\"https://nodejs.org/id/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"10\",{\"rel\":\"alternate\",\"hrefLang\":\"ja\",\"href\":\"https://nodejs.org/ja/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"11\",{\"rel\":\"alternate\",\"hrefLang\":\"ko\",\"href\":\"https://nodejs.org/ko/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"12\",{\"rel\":\"alternate\",\"hrefLang\":\"pt\",\"href\":\"https://nodejs.org/pt/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"13\",{\"rel\":\"alternate\",\"hrefLang\":\"tr\",\"href\":\"https://nodejs.org/tr/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"14\",{\"rel\":\"alternate\",\"hrefLang\":\"uk\",\"href\":\"https://nodejs.org/uk/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"15\",{\"rel\":\"alternate\",\"hrefLang\":\"zh-cn\",\"href\":\"https://nodejs.org/zh-cn/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"16\",{\"rel\":\"alternate\",\"hrefLang\":\"zh-tw\",\"href\":\"https://nodejs.org/zh-tw/learn/asynchronous-work/event-loop-timers-and-nexttick\"}],[\"$\",\"link\",\"17\",{\"rel\":\"alternate\",\"type\":\"application/rss+xml\",\"href\":\"https://nodejs.org/en/feed/blog.xml\"}],[\"$\",\"meta\",\"18\",{\"property\":\"og:title\",\"content\":\"Node.js — The Node.js Event Loop\"}],[\"$\",\"meta\",\"19\",{\"property\":\"og:description\",\"content\":\"Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.\"}],[\"$\",\"meta\",\"20\",{\"property\":\"og:image\",\"content\":\"https://nodejs.org/en/next-data/og/announcement/Node.js%20%E2%80%94%20The%20Node.js%20Event%20Loop\"}],[\"$\",\"meta\",\"21\",{\"name\":\"twitter:card\",\"content\":\"summary\"}],[\"$\",\"meta\",\"22\",{\"name\":\"twitter:creator\",\"content\":\"@nodejs\"}],[\"$\",\"meta\",\"23\",{\"name\":\"twitter:title\",\"content\":\"Node.js — The Node.js Event Loop\"}],[\"$\",\"meta\",\"24\",{\"name\":\"twitter:description\",\"content\":\"Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.\"}],[\"$\",\"meta\",\"25\",{\"name\":\"twitter:image\",\"content\":\"https://nodejs.org/static/images/logo-hexagon-card.png\"}],[\"$\",\"meta\",\"26\",{\"name\":\"twitter:image:alt\",\"content\":\"The Node.js Hexagon Logo\"}],[\"$\",\"link\",\"27\",{\"rel\":\"icon\",\"href\":\"/static/images/favicons/favicon.png\"}]]\n"])</script><script>self.__next_f.push([1,"7:null\n"])</script><script>self.__next_f.push([1,"37:I[19296,[\"2916\",\"static/chunks/f6578f9b-928ce6e3d7b11107.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"1917\",\"static/chunks/58f38233-9006bbab35cec14d.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"9223\",\"static/chunks/08e9f8d4-9d4b7d86103608ef.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"269\",\"static/chunks/9dc4cb83-735a522d5237a7e2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"154\",\"static/chunks/154-7f9758f0b6c172e7.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6988\",\"static/chunks/6988-83825c21c9d8d3e4.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"4176\",\"static/chunks/4176-40784cc0ab5352f2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"8327\",\"static/chunks/8327-c76ff2fd75830f33.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"3454\",\"static/chunks/3454-b9980bc5c0ad948c.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"288\",\"static/chunks/288-53ef86460a26cce2.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"5040\",\"static/chunks/5040-53817b35b8adfe64.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"6973\",\"static/chunks/6973-255b027a03a09328.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"2683\",\"static/chunks/2683-b34bf85392621e38.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"7575\",\"static/chunks/7575-5c1c85db56589dec.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\",\"465\",\"static/chunks/app/%5Blocale%5D/page-bc4f98ff618ebf0a.js?dpl=dpl_FfeGbCFF1gV4URbnAfV5sixp8GSF\"],\"default\"]\n18:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#the-nodejs-event-loop\",\"locale\":\"$undefined\",\"localeCookie\":{\"name\":\"NEXT_LOCALE\",\"maxAge\":31536000,\"sameSite\":\"lax\"},\"unprefixed\":\"$undefined\",\"children\":\"The Node.js Event Loop\"}]\n19:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#what-is-the-event-loop\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"What is the Event Loop?\"}]\n1a:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#event-loop-explained\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"Event Loop Explained\"}]\n1c:[\"$\",\""])</script><script>self.__next_f.push([1,"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#timers\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[\"$\",\"strong\",null,{\"children\":\"timers\"}]}]\n1d:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#poll\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[\"$\",\"strong\",null,{\"children\":\"poll\"}]}]\n1e:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#phases-overview\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"Phases Overview\"}]\n1f:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#phases-in-detail\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"Phases in Detail\"}]\n20:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#timers\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"timers\"}]\n21:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#poll\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[[\"$\",\"strong\",null,{\"children\":\"poll\"}],\" phase\"]}]\n22:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#pending-callbacks\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"pending callbacks\"}]\n23:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#poll\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"poll\"}]\n24:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#check\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"check\"}]\n25:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#close-callbacks\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:"])</script><script>self.__next_f.push([1,"props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"close callbacks\"}]\n26:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#setimmediate-vs-settimeout\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}],\" vs \",[\"$\",\"code\",null,{\"children\":\"setTimeout()\"}]]}]\n2b:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#processnexttick\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}]}]\n2c:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#understanding-processnexttick\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[\"Understanding \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}]]}]\n2d:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#why-would-that-be-allowed\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":\"Why would that be allowed?\"}]\n2e:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#processnexttick-vs-setimmediate\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\" vs \",[\"$\",\"code\",null,{\"children\":\"setImmediate()\"}]]}]\n2f:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"#why-use-processnexttick\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"children\":[\"Why use \",[\"$\",\"code\",null,{\"children\":\"process.nextTick()\"}],\"?\"]}]\n30:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"/en/learn/asynchronous-work/discover-javascript-timers\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"className\":\"CrossLink_crossLink__OH1_N\",\"children\":[[\"$\",\"span\",null,{\"className\""])</script><script>self.__next_f.push([1,":\"CrossLink_header__opkEd\",\"children\":[[\"$\",\"svg\",null,{\"xmlns\":\"http://www.w3.org/2000/svg\",\"viewBox\":\"0 0 24 24\",\"fill\":\"currentColor\",\"aria-hidden\":\"true\",\"data-slot\":\"icon\",\"ref\":\"$undefined\",\"aria-labelledby\":\"$undefined\",\"className\":\"CrossLink_icon__Tm_uZ\",\"children\":[null,[\"$\",\"path\",null,{\"fillRule\":\"evenodd\",\"d\":\"M7.72 12.53a.75.75 0 0 1 0-1.06l7.5-7.5a.75.75 0 1 1 1.06 1.06L9.31 12l6.97 6.97a.75.75 0 1 1-1.06 1.06l-7.5-7.5Z\",\"clipRule\":\"evenodd\"}]]}],\"Prev\"]}],[\"$\",\"span\",null,{\"className\":\"CrossLink_content__AuXgK\",\"children\":\"Discover JavaScript Timers\"}]]}]\n31:[\"$\",\"$L37\",null,{\"ref\":\"$undefined\",\"defaultLocale\":\"$undefined\",\"href\":\"/en/learn/asynchronous-work/the-nodejs-event-emitter\",\"locale\":\"$undefined\",\"localeCookie\":\"$18:props:localeCookie\",\"unprefixed\":\"$undefined\",\"className\":\"CrossLink_crossLink__OH1_N\",\"children\":[[\"$\",\"span\",null,{\"className\":\"CrossLink_header__opkEd CrossLink_reverse__CVtKH\",\"children\":[[\"$\",\"svg\",null,{\"xmlns\":\"http://www.w3.org/2000/svg\",\"viewBox\":\"0 0 24 24\",\"fill\":\"currentColor\",\"aria-hidden\":\"true\",\"data-slot\":\"icon\",\"ref\":\"$undefined\",\"aria-labelledby\":\"$undefined\",\"className\":\"CrossLink_icon__Tm_uZ\",\"children\":[null,[\"$\",\"path\",null,{\"fillRule\":\"evenodd\",\"d\":\"M16.28 11.47a.75.75 0 0 1 0 1.06l-7.5 7.5a.75.75 0 0 1-1.06-1.06L14.69 12 7.72 5.03a.75.75 0 0 1 1.06-1.06l7.5 7.5Z\",\"clipRule\":\"evenodd\"}]]}],\"Next\"]}],[\"$\",\"span\",null,{\"className\":\"CrossLink_content__AuXgK CrossLink_reverse__CVtKH\",\"children\":\"The Node.js Event Emitter\"}]]}]\n"])</script></body></html>