JavaScript Node.js Interview Questions: Event Loop and Async Behavior

💡 My Recent Interview Experience – JavaScript & Node.js Deep Dive Recently, I came across some really interesting event loop and async behavior questions during an interview. Thought of sharing them here — great for anyone preparing for frontend/backend roles 🚀 🔹 Question 1: Promise Behavior 1) const promise1 = new Promise((resolve, reject) => { console.log(1) resolve('resolve1') }) const promise2 = promise1.then((res) => { console.log(res) }) console.log('promise1:', promise1); console.log('promise2:', promise2); 👉 What will be the output? 2) console.log("start"); setTimeout(() => { console.log("timeout"); }, 0); Promise.resolve().then(() => { console.log("promise1"); }).then(() => { console.log("promise2"); }); process.nextTick(() => { console.log("nextTick"); }); console.log("end"); 👉 What will be the output? 🔹 Question 2: Event Loop Priority setTimeout(() => console.log("timeout1"), 0); setImmediate(() => console.log("immediate1")); Promise.resolve().then(() => console.log("promise")); process.nextTick(() => console.log("nextTick")); console.log("sync"); 👉 Predict the output order. 🔹 Question 3: Tricky Execution Order 😄 console.log("A"); setTimeout(() => { console.log("B"); Promise.resolve().then(() => { console.log("C"); }); process.nextTick(() => { console.log("D"); }); }, 0); setImmediate(() => { console.log("E"); }); Promise.resolve().then(() => { console.log("F"); }); process.nextTick(() => { console.log("G"); }); console.log("H"); 👉 What is the final output? 🔹 Question 4: Nested Microtasks & nextTick Promise.resolve().then(() => { console.log("p1"); process.nextTick(() => { console.log("nt1"); }); Promise.resolve().then(() => { console.log("p2"); }); }); process.nextTick(() => { console.log("nt2"); }); console.log("end"); 👉 Guess the output. 🔹 Question 5: I/O + setImmediate vs setTimeout const fs = require("fs"); fs.readFile(__filename, () => { console.log("readFile"); setTimeout(() => console.log("timeout"), 0); setImmediate(() => console.log("immediate")); }); console.log("start"); 👉 What will be the output? 🧠 These questions really test your understanding of: Event Loop phases Microtasks vs Macrotasks process.nextTick priority setImmediate vs setTimeout Promise chaining behavior 💬 Drop your answers in the comments—let's discuss! #javascript #nodejs #frontend #interviewpreparation #webdevelopment #softwareengineering #codinginterview

To view or add a comment, sign in

Explore content categories