Understanding Node.js: The Event Loop The code below helps illustrate how Node.js internals work and how different queues are prioritized. The output will always start with A F D E, because: - A and F are synchronous instructions and run immediately. - process.nextTick() callbacks run before any other microtasks. - Promise.then() callbacks run after nextTick, but still before the Event Loop continues. The Event Loop phases are: - Timers - Pending Callbacks - Idle / Prepare - Poll - Check - Close Callbacks One important detail: the execution order between setTimeout and setImmediate is not guaranteed when both are scheduled from the main module. - setTimeout runs in the timers phase - setImmediate runs in the check phase. Depending on how the event loop advances, the output may be: - AFDEBC - AFDECB Understanding these details helps avoid subtle bugs and makes async behavior predictable. Which part of the Node.js event loop confused you the most when you first learned it? #NodeJS #NodeJSTips #NodeJSInternals #BackendEngineering #JavaScript
Node.js Event Loop Explained: Timers, Callbacks, and Priorities
More Relevant Posts
-
👉 Event Loop in Node.js JavaScript is single threaded, but Node.js can handle many tasks at the same time. This is possible because of the Event Loop. 👉 What Event Loop does -Continuously checks if the call stack is empty -Picks the next task from queues -Executes it without blocking the main thread 👉 How it works -Synchronous code runs in the call stack -Async tasks like timers, file system, and APIs go to background Once the stack is empty, Event Loop pushes tasks back to execution 👉 Execution order -Microtask queue → Promises, process.nextTick -Timers queue → setTimeout, setInterval -I O queue → file system, network calls -Check queue → setImmediate 👉 Why it matters -Handles thousands of requests efficiently -Keeps the application fast and non blocking 👉 Key point Node.js is single threaded, but highly concurrent because of the Event Loop. #nodejs #javascript #eventloop #backenddevelopment #webDevelopment
To view or add a comment, sign in
-
🚨 𝐇𝐨𝐰 𝐭𝐨 𝐅𝐢𝐱 𝐓𝐲𝐩𝐞𝐒𝐜𝐫𝐢𝐩𝐭 𝐢𝐬𝐨𝐥𝐚𝐭𝐞𝐝𝐌𝐨𝐝𝐮𝐥𝐞𝐬 𝐄𝐫𝐫𝐨𝐫 𝐢𝐧 𝐍𝐞𝐱𝐭.𝐣𝐬? If your Next.js build fails with this message: Re-exporting a type when isolatedModules is enabled requires using export type Don’t worry, it’s not a complex bug. It’s just about being clear with TypeScript. 𝐖𝐡𝐚𝐭’𝐬 𝐀𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐇𝐚𝐩𝐩𝐞𝐧𝐢𝐧𝐠? When `isolatedModules` is enabled, each file is compiled independently. So TypeScript must know exactly: - what is real JavaScript code that needed at runtime. - what is only for type checking. If you export a type like a normal value, the compiler expects it to exist in JavaScript but it doesn’t. That’s why the build fails. ❌ 𝐖𝐫𝐨𝐧𝐠 𝐖𝐚𝐲 export { Profile, saveProfile } from './helpers'; If `Profile` is a type and `saveProfile` is a function, this causes errors in isolated builds. ✅ 𝐂𝐨𝐫𝐫𝐞𝐜𝐭 𝐖𝐚𝐲 // runtime exports export { saveProfile } from './helpers'; // type-only exports export type { Profile } from './helpers'; Easy Rule to Remember: - Use `export {}` for functions, components, constants - Use `export type {}` for types and interfaces Have you encountered this issue? 😀 #creowis #TypeScript #NextJS #FrontendDev #EngineeringTips #WebDevelopment
To view or add a comment, sign in
-
-
Most devs get this wrong 👀 Especially folks with 1–2 years of JavaScript experience. No frameworks. No libraries. No async magic. Just core JS fundamentals. Question 👇 console.log([] == ![]); ❓ What will this print? A. true B. false C. undefined D. Throws an error Why this matters JavaScript doesn’t always behave the way we expect it to. This one tests how well you understand: type coercion truthy vs falsy values how == really works under the hood When fundamentals aren’t clear: we predict the wrong output bugs feel random debugging turns into guesswork Good developers don’t just write code. They understand language behavior. Drop your answer in the comments 👇 Did this one make you pause? #JavaScript #JSFundamentals #JavaScriptQuiz #WebDevelopment #FrontendDeveloper #FullStackDeveloper #LearnJavaScript #DevelopersOfLinkedIn #DevCommunity #ReactDeveloper #NodeDeveloper #VibeCode
To view or add a comment, sign in
-
-
Most devs get this wrong 👀 Even developers with 2–5+ years of experience. No frameworks. No libraries. Just JavaScript fundamentals. Question 👇 async function test() { console.log(1); await Promise.resolve(); console.log(2); } console.log(3); test(); console.log(4); ❓ What will be the output order? A. 1 2 3 4 B. 3 1 2 4 C. 3 1 4 2 D. 1 3 4 2 Why this matters If you don’t understand microtasks vs call stack, async/await bugs feel random. Strong developers don’t guess. They understand how JavaScript actually runs. Drop your answer in the comments 👇 Did you get it right on the first try? #JavaScript #JSFundamentals #AsyncAwait #WebDevelopment #FrontendDeveloper #FullStackDeveloper #CodingInterview #DevelopersOfLinkedIn #DevCommunity #LearnJavaScript #VibeCode
To view or add a comment, sign in
-
-
🔁 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 (𝗦𝗶𝗺𝗽𝗹𝗶𝗳𝗶𝗲𝗱) ▪ Initial code execution ▪ Optionally register callbacks ▪ Execute callbacks ▪ Callbacks may register more callbacks ▪ Loop continues until all queues are empty 📌 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗯𝗲𝗵𝗮𝘃𝗶𝗼𝗿: ▪ If one callback executes, another callback is picked from the queue ▪ Initial phases execute first ▪ After that, the loop starts running ▪ Initial code mainly registers callbacks ❓ 𝗣𝗿𝗼𝗺𝗶𝘀𝗲𝘀 & 𝗮𝘀𝘆𝗻𝗰/𝗮𝘄𝗮𝗶𝘁: ▪ They are also callbacks ▪ Will be covered separately 💬 Comment below with your thoughts. and follow, like, and share for more content like this! #NodeJS #Javascript #BackendEngineering #EventLoop
To view or add a comment, sign in
-
Most JavaScript devs pause on this 👀 Even with async/await experience. No frameworks. No libraries. Just JavaScript fundamentals. Question 👇 async function test() { try { return Promise.reject("Error"); } catch (e) { console.log("caught"); } } test().catch(console.log); ❓ What will be printed to the console? A. caught B. Error C. Nothing D. Both caught and Error Why this matters Many developers assume: try/catch handles all errors inside async functions Promise.reject() behaves like throw That assumption is wrong. When fundamentals aren’t clear: error handling feels unpredictable bugs slip through silently debugging turns into guesswork Strong developers don’t guess. They understand how async functions actually propagate errors. 👇 Drop your answer in the comments Did this one make you think twice? #JavaScript #JSFundamentals #AsyncAwait #Promises #WebDevelopment #FrontendDeveloper #FullStackDeveloper #CodingInterview #DevelopersOfLinkedIn #DevCommunity #VibeCode
To view or add a comment, sign in
-
-
Most devs get this wrong 👀 No frameworks. No libraries. No async tricks. Just pure JavaScript fundamentals. Question 👇 const user = { name: "JS" }; Object.freeze(user); user.name = "React"; console.log(user.name); ❓ What will this log? A. "React" B. "JS" C. undefined D. Throws an error Why this matters Many developers believe Object.freeze() protects objects from all changes. It doesn’t. It prevents mutation — but it does NOT throw an error in non-strict mode. When fundamentals aren’t clear: bugs slip into production silently debugging becomes guesswork confidence in code drops Strong developers don’t just use features. They understand how JavaScript actually behaves. Drop your answer in the comments 👇 Did this one surprise you? #JavaScript #JSFundamentals #WebDevelopment #FrontendDeveloper #FullStackDeveloper #DevelopersOfLinkedIn #CodingInterview #Programming #DevCommunity #LearnJavaScript #VibeCode
To view or add a comment, sign in
-
-
JavaScript is NOT enough by itself 🚨 JavaScript cannot: ❌ Access files ❌ Use timers ❌ Make network calls ❌ Talk to the OS That’s why we need a JavaScript Runtime Environment. A runtime provides: ✅ JS Engine ✅ Web / System APIs ✅ Event Loop ✅ Memory Management Examples: 🌐 Browser Runtime (Chrome, Firefox) 🖥️ Node.js (run JS outside the browser) 👉 JavaScript = Language 👉 Runtime = Execution Environment If you’re learning JS and ignoring runtimes, you’re missing the real picture. 💡 Save this post for interview. Nishant Pal #JavaScript #WebDevelopment #NodeJS #Frontend #Backend #Programming #Coding #InterviewPrep
To view or add a comment, sign in
-
-
This looks obvious… until it isn’t 👀 Most Node.js devs answer this too fast. No frameworks. No libraries. Just JavaScript fundamentals. Question 👇 function createCounter() { let count = 0; return { inc() { count++; }, get() { return count; } }; } const counter1 = createCounter(); const counter2 = createCounter(); counter1.inc(); counter1.inc(); counter2.inc(); console.log(counter1.get(), counter2.get()); ❓ What will be printed? A. 2 1 B. 3 3 C. 1 1 D. 2 2 👇 Drop ONE option only in the comments Why this matters Many developers know closures… but still get confused about: shared vs isolated state how function scope actually works why two “similar” objects behave differently When fundamentals aren’t clear: bugs feel random state becomes unpredictable debugging turns into guesswork Good engineers don’t just write code. They understand why it behaves this way. Did this one make you pause? 👀 #NodeJS #JavaScript #WebDevelopment #fullstackDeveloper #CodingChallenge #reactDeveloper 😊 #ProgrammingFundamentals #Closures #StateManagement #SoftwareEngineering #Debugging #DeveloperCommunity
To view or add a comment, sign in
-
-
Sharing my recent learning on error handling while search operation- If an API error happens during search and we don’t handle it, RxJS closes the stream. After that, even if the user types again, the search won’t run. Why this happens? Because in RxJS, an error ends the observable. We fix it by proper error handling - We handle the error inside the API call (inside switchMap), not outside, using catchError, and return a safe value (like an empty list). This way, only that API call fails — not the whole search stream. So one error happens, then User types again and Search still works. Some time revising things get more clear understanding. ---------------- Read my medium article, I write about Angular and JavaScript. https://lnkd.in/gUYuypyT #angular #javascript
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development