What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

The JavaScript event loop explains why synchronous logs, Promise callbacks, timers, and I/O callbacks appear in a particular order. On a given JavaScript agent, the current job runs to completion; the host environment schedules later work, and microtasks are drained before the runtime moves on to another task. That model is useful in both browsers and Node.js, but their host-specific scheduling rules differ.

The key to debugging async code is to mark where synchronous execution ends, identify which kind of queue or host phase receives each callback, and then verify the sequence with runtime tools. A callback can be asynchronous to schedule and still block everything once it starts running.

How does the JavaScript event loop work?

The event loop coordinates when JavaScript jobs run; it does not make callbacks run in parallel with the current call stack. JavaScript engines execute code, while the host environment—such as a browser or Node.js—provides timers, event and I/O handling, and scheduling. On a given agent, a job’s synchronous code runs to completion before another JavaScript job can take its turn. MDN’s execution model describes this relationship between jobs, the execution context, and the host.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use four concepts to read an async trace:

  • Call stack: the synchronous functions currently executing.
  • Task and microtask queues: host-scheduled work that becomes eligible after the current stack unwinds.
  • Run-to-completion: JavaScript does not switch to another job halfway through the synchronous part of the current job.
  • Host rules: browsers and Node.js decide how their scheduling mechanisms interact with their own work.

In the browser teaching model, one task runs, the runtime drains microtasks, and the browser may then render before starting another iteration. “May” matters: a paint is not guaranteed after every task. Browser event loops also vary by context—windows, workers, and worklets have distinct arrangements—so the simplified loop is a guide to ordering, not a promise that every kind of browser work uses one universal FIFO queue. MDN’s in-depth microtask guide and the WHATWG HTML Standard describe the more detailed host model.

Popular on Amazon
Silkland 40Gbps USB C 90 Degree Right Angle Adapter 2 Pack, 240W USB4 | Male to Female Extender, 8K@60Hz, Thunderbolt 4/3, Steam Deck, ROG Ally, MacBook, Switch 2, iPhone 18/17/16/15, Quest 3 VR
  • 【90 Degree Full Function USB C Adapter】Supports the latest USB4 standard for up to 40Gbps of bandwidth, 8K video support, and up to 240W Fast charging; Support USB PD 3.1 Extended Power Range (EPR) including 140W, 180W and 240W. Your productivity is easily doubled. *Warm Tip: For the best fit, please make sure the USB-C port is fully exposed; thick or bulky cases may affect compatibility.
  • 【Reliable Quality】Made from high-quality, scratch-resistant aluminum alloy, delicate metal frame. get excellent wear resistance and heat dissipation. Non-Slip Design for easier insertion and removal.
  • 【Small and portable】Ultra thin size, ultra compact design, a petite physique that matches the minimalist design of your modern laptops. Coming with a slim figure that won't block the port next to the one it's plugged into.
  • 【Compatibility】Widely work with most usb c, thunderbolt 4/3 devices. Compatibilit for ROG Ally, Steam Deck, Switch, MacBook Pro, Quest 3 VR headset, Surface Book 2, iPhone 18/17/16/15, Samsung Galaxy S23/S22/S21, Note 20/10, Google Pixel, ChromeBook, etc.
  • 【Excellence Customer Support】If you experience any issues or cable failure, simply reach out to our dedicated customer service team. We provide a prompt response within 18 hours, providing you with the necessary technical support to resolve any concerns.

A useful sketch is:

  1. Run the current JavaScript job until its stack is empty.
  2. Run eligible microtasks until none remain, including microtasks added while draining.
  3. Allow the host to perform other work, such as rendering where applicable, and select further scheduled work.

This is an orchestration model, not a second JavaScript thread that takes over while a callback is busy. A long synchronous callback still occupies its agent’s execution thread.

What is the difference between the task queue and the microtask queue?

Tasks are broader units of scheduled work, while microtasks are a higher-priority set of callbacks drained after a job and before the runtime proceeds to another task. That priority explains many “why did this log come first?” puzzles.

Initial script execution, asynchronously dispatched events, and timer or interval callbacks are examples of tasks. Promise reactions registered with .then(), .catch(), or .finally(), as well as callbacks passed to queueMicrotask(), are microtasks. When a task finishes and the JavaScript stack is empty, the runtime processes microtasks until the queue is empty. If one microtask schedules another, the new one is processed during that drain rather than waiting for the next task. MDN’s microtask guide explains the scheduling behavior and its implications.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does Promise.then run before setTimeout(…, 0)?

A zero-delay timer does not run in the middle of the current script. Its callback is scheduled as a later task; the Promise reaction is a microtask, which is processed before the runtime proceeds to that task.

Also popular on Amazon
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
console.log("sync");
setTimeout(() => console.log("task"), 0);
Promise.resolve().then(() => console.log("microtask"));
console.log("end");

The conceptual output is sync, end, microtask, then task. The first two logs execute synchronously in source order. The Promise reaction waits until the current job finishes, then runs in the microtask drain. The timer callback is eligible as a later task. This is why “zero” should be read as a timer delay setting, not as “run immediately.” MDN’s Promise guide covers Promise reaction scheduling.

Can microtasks delay other work?

Yes. Since the runtime drains microtasks until the queue is empty, code that continually schedules another microtask can prevent it from moving on to later tasks. In a browser, that can also delay opportunities for rendering. Use queueMicrotask() when a microtask boundary is specifically useful; it is not a general replacement for yielding to a later task.

Why does await change the order of my logs?

await suspends the continuation of its async function and schedules that continuation as a microtask when the awaited value settles. It does not make every line in an async function run later: code before the first await runs synchronously.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Promise executor also runs synchronously when its Promise is constructed. The callbacks registered through .then(), .catch(), and .finally() run later as microtasks. await follows the same essential continuation rule even when the awaited value is already a resolved Promise or is not a Promise. MDN’s await reference describes how the expression pauses and resumes an async function; the Promise guide describes reaction scheduling.

Also popular on Amazon
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

For example, the log before await happens during the current synchronous job. The log after it happens when the function’s continuation is resumed, so other synchronous statements in the surrounding job can run first. When tracing, mark the exact await and treat the rest of the function as a continuation; do not assume that calling an async function makes its entire body asynchronous from the first line.

How do browser event loops handle rendering and workers?

Browser JavaScript and interface work share the main thread in a window, so a long-running callback can delay input handling and rendering. After a task and its microtasks, the browser may render, but it does not promise a paint at every such boundary.

Workers run in separate agents and have their own event-loop arrangements, unlike code sharing the window’s main thread. This distinction matters when diagnosing a page that feels stuck: scheduling a callback for later does not free the main thread while a long synchronous callback is running, whereas moving suitable work into a worker changes which agent performs it. The browser’s event-loop details are host behavior, not a universal JavaScript-engine feature. MDN’s in-depth guide distinguishes window, worker, and worklet loops.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How is the Node.js event loop different from the browser model?

Node.js describes its event loop in phases rather than only as a browser-style task/microtask sequence. Its documented phases include timers, pending callbacks, poll, check, and close callbacks; the poll phase handles I/O-related callbacks, while setImmediate() runs in the check phase. Node.js’s event-loop guide explains these phases and their ordering.

Also popular on Amazon
BENFEI USB C Hub 5-in-1 with 4K HDMI(Certified), 100W Power Delivery, 3 USB-A, Silicone Cable, Aluminum Case Compatible with MacBook Pro/Air, iPad Pro, iMac, iPhone 15 Pro/Pro Max, XPS, Thinkpad
  • Portable and powerful USB-C HUB: BENFEI USB Type-C HUB, with super-soft and knot-free silicone woven design cable, meets most mobile office needs. Compact, lightweight, stylish, and powerful portable USB C Hub equipped with 1 x HDMI port, 1 x 100W charging, and 3 x USB ports. 18-month warranty, 24-hour response, to ensure you feel at ease when using our product.
  • Design centered on comfort and reliability: Thanks to BENFEI's end-to-end in-house cable production capability, in-house PCBA and assembly capability, using the industry's most advanced silicone woven design and process, 20cm cable in length, no knots, super-soft, the HUB is easy to use in all scenarios: laptop, tablet, stand etc. Super-soft, 25000+ life cycles, to meet your daily carrying and office needs.
  • 100W Charging: Support up to 90W USB C pass-through charging via Type-C port to keep your laptop powered. 10W is reserved for other interface operations. No data and video function on the Type-C port.
  • 4K HDMI Display: The HDMI port supports media display at resolutions up to 4K 30Hz, keeping every incredible moment detailed and ultra vivid. Please note that the C port of the Host device needs to support video output.
  • Transfer Files in Seconds: Transfer files and from your laptop at speeds up to 10 Gbps with USB A 3.2 port. Extra 2 USB A 2.0 ports are perfectly for your keyboards and mouse.
Question Browser-oriented model Node.js model
How is scheduled work described? Tasks and microtasks; browser event loops may have one or more task queues. Phases including timers, pending callbacks, poll, check, and close callbacks.
What runs after the current job? Microtasks drain before the runtime proceeds to another task. process.nextTick() has its own priority behavior and is not a normal event-loop phase.
What host work is relevant? The browser may render after microtasks; windows also handle interface work. There is no browser paint step; poll processes I/O-related callbacks.
What can a long callback affect? Responsiveness, input handling, and rendering opportunities. Throughput and the opportunity for other clients and I/O to make progress.

How should I reason about process.nextTick()?

Node.js documents process.nextTick() as running after the current operation and before the event loop continues through its phases; it is not simply another phase alongside timers or poll. Recursive use can starve I/O by preventing the loop from reaching poll. Node’s guide recommends considering setImmediate() when a clearer boundary is easier to reason about.

Does setTimeout(…, 0) always run before setImmediate()?

No single result should be promised for calls scheduled at top level in the main module: Node.js documents that their ordering can be non-deterministic. When both are scheduled inside an I/O cycle, Node.js documents that setImmediate() runs first. Identify the scheduling context before drawing conclusions from a log order.

How do I trace async callbacks in Chrome DevTools?

Start with a labeled reproduction, then use breakpoints to stop at the scheduling or callback locations and a Performance recording to inspect where time and execution went. These tools help verify a queue-based explanation against the browser’s actual call path.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Label the trace. Add distinct log labels at the synchronous call site, the callback, and important boundaries such as Promise reactions, timers, and request completion. Keep the example small enough that each label has an identifiable source line.
  2. Mark the boundaries. Annotate which statements are synchronous, which callbacks are microtasks, and which are tasks or host completions. Do not treat a network completion as if it were a Promise reaction; the host event and the Promise continuation can be separate points in the chain.
  3. Pause at known code. In Chrome DevTools, open Sources and set a line-of-code breakpoint where a callback is scheduled or executed. For event-driven paths, use event-listener breakpoints to pause on the listener code after its event fires. Chrome documents this in JavaScript breakpoints.
  4. Catch request boundaries. Use XHR/fetch breakpoints when the question is where request-related code begins or resumes. Use exception breakpoints when the failure is a thrown error or rejection rather than an unexpected order.
  5. Record execution. In the Performance panel, record a trace and inspect the Main track, flame chart, call stack, initiator links, and long-task markers. This helps connect a callback to its caller and identify where the main thread spent time. Chrome’s Performance documentation describes the panel.
  6. Compare trace to model. If the observed order differs from your sketch, check the host context, whether a callback is actually a microtask or task, and whether the trace contains additional scheduling steps. In Node.js, map callbacks to documented phases rather than applying browser ordering assumptions.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why can async code still block a browser or Node.js server?

“Async” describes how work is arranged, not how much CPU time a callback consumes after it begins. A callback reached through a timer, Promise, or I/O path still runs its JavaScript synchronously until it returns or yields, so a long callback can occupy its agent and delay other work.

Also popular on Amazon
𝟒𝐏𝐚𝐜𝐤 𝐔𝐩𝐠𝐫𝐚𝐝𝐞𝐝 USB to USB C Adapter,Type-C Female to A Male Charger Converter for iPhone 18 Charger,iWatch Ultra SE 9 10,iPhone 17 Pro Max 16 15 14,Samsung S25,AirPods,iPad,Car,Xbox
  • 【Read before You Buy】 Note: This adapter is USB 2.0 version and cannot transmit HDM, VGA, or other video signals. Nevertheless, they are fully capable of charging devices,duplicating files, and can be seamlessly integrated with USB-C earphones, wireless network adapters, and SD card adapters.
  • 【Easy to Use and Safe】Thanks to modern manufacturing techniques, we've made these adapters sufficiently compact so that they can remain inserted in your device without noticeable protrusion or inconvenience. This means you don't have to constantly plug and unplug them, reducing the likelihood of wear and misalignment, and making them more convenient to carry around.
  • 【Reliable Quality】Engineered for enhanced durability, durable aluminum alloy shell material,get excellent wear resistance and heat dissipation,the male USB C and female USB connectors & the male USB and female USB C connectors are designed to be plugged and unplugged up to 20000 times. Provides data transfer, device charging, memory space expansion, and additional computer peripherals.
  • 【Think more for you】Do you need to connect a USB-C-enabled device to an odd device that only has USB-A ports? Then these USB-A male to USB-C female adapters are made for you.Simply insert the adapter into the USB-A port on the device, and you can instantly connect your USB-C device to the device that originally didn't have the correct connection port.
  • 【Trusted xiwxi Quality and Service】As a responsible brand seller, you will receive xiwxi's 12-month Quality assurance and lifetime friendly customer service, you're not just buying a product,you're investing in peace of mind with all-round quality and exceptional customer service.

MDN’s performance guidance treats a task longer than 50 milliseconds as a long task for responsiveness purposes; Chrome DevTools highlights the portion over 50 milliseconds in red in the Performance panel. This is a current performance heuristic and tooling threshold, not a claim that every task below it is imperceptible or every task above it causes the same user-visible problem. MDN’s JavaScript performance guide and Chrome’s Performance documentation describe the guidance and marker.

In a browser, expensive work on the main thread can delay input and rendering. Breaking suitable work into smaller tasks gives the browser opportunities to handle other work between chunks. In Node.js, long event-loop callbacks reduce throughput because other clients do not get a turn; for suitable CPU-heavy work, use the Worker Pool or another worker strategy rather than assuming an asynchronous API makes the JavaScript computation non-blocking. Node.js discusses this risk in its guide to not blocking the event loop.

What should I check when the trace does not match my expectation?

First confirm the scheduling context, then check whether the work is synchronous, a microtask, a task, or host-specific. A compact checklist prevents common misdiagnoses:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Did the current stack finish? A callback cannot interleave with the synchronous statements already running on the same agent.
  • Is the callback a Promise reaction or a timer/event callback? The former is a microtask; the latter is generally task or host-phase work.
  • Did a microtask add more microtasks? The drain continues until the microtask queue is empty, so later tasks may wait.
  • Is this browser code or Node.js code? Node’s phases and process.nextTick() behavior cannot be collapsed into the browser’s simplified task-loop picture.
  • Is the issue ordering or duration? A trace may show the expected order while still revealing a long callback that blocks progress.
  • Are you inferring a browser paint from a callback boundary? Rendering is a host decision; do not assume every task is followed by a paint.

When should Node.js async_hooks be used to trace execution?

async_hooks can help advanced Node.js investigations that need async-resource or execution-context information, but it should not be treated as a definitive, effortless map of every Promise continuation. Node.js labels the low-level hook APIs experimental and notes that Promise execution tracking is not precise by default. Consult the current Node.js async_hooks documentation before relying on it; for basic ordering puzzles, phase reasoning and small labeled reproductions are often easier to interpret.

Frequently Asked Questions

Does JavaScript run Promise callbacks on another thread?

No: a Promise reaction is scheduled to run later, but its JavaScript still executes on the relevant agent when it gets its turn. Separate workers are a different execution arrangement; a Promise alone does not make CPU-heavy JavaScript parallel.

Why can a page freeze even when the slow operation uses a Promise?

A Promise can represent an operation that completes later, but any CPU-intensive JavaScript in its executor or continuation still occupies the thread while it runs. The relevant question is what the callback computes, not whether the surrounding API returns a Promise.

Can I rely on a timer firing exactly after its requested delay?

No. A timer delay makes its callback eligible for later scheduling; it does not reserve an exact execution time or interrupt JavaScript already on the stack. Other queued work and host scheduling affect when the callback actually runs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply. Prices as of December 6, 2026 10:01 am GMT.