JavaScript Event Loop — Microtask vs Macrotask setTimeout(() => console.log("Timeout"), 0) Promise.resolve().then(() => console.log("Promise")) Output: Promise Timeout Why? Because: • Promise callbacks go to the Microtask queue • setTimeout goes to the Macrotask queue • Microtasks run before Macrotasks Understanding the event loop helps avoid async confusion and race conditions. If you know this, debugging async becomes much easier. #JavaScript #EventLoop #AsyncProgramming #Frontend
JavaScript Event Loop: Microtask vs Macrotask
More Relevant Posts
-
learned about the switch statement in JavaScript. It is used when we need to check multiple conditions. Example: let day = 2; switch(day){ case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; default: console.log("Another day"); } Switch statements make code cleaner when handling many conditions. #JavaScript #CodingJourney #FrontendDeveloper
To view or add a comment, sign in
-
-
🚀 𝗪𝗵𝗼 𝗘𝘅𝗲𝗰𝘂𝘁𝗲𝘀 𝗙𝗶𝗿𝘀𝘁 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁? 🚀 I’ve just shared a handy guide—“Who Executes First in JavaScript: Synchronous, Microtasks & Macrotasks”—to help you predict exactly when your code runs and avoid those nagging timing bugs. 𝗠𝗮𝗶𝗻 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀: - 𝗦𝘆𝗻𝗰 𝗳𝗶𝗿𝘀𝘁: Plain console.log calls run immediately in sequence. - 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸𝘀 𝗻𝗲𝘅𝘁: Promises (and await continuations) always beat macrotasks. - 𝗠𝗮𝗰𝗿𝗼𝘁𝗮𝘀𝗸𝘀 𝗹𝗮𝘀𝘁: Even setTimeout(..., 0) waits until after all sync and microtasks. Want to see this in action? Check out the slide deck for live code examples—and let me know which timing gotcha has tripped you up the most! #javascript
To view or add a comment, sign in
-
⚡ Microtasks vs Macrotasks in JavaScript (Event Loop) 🔹 Macrotasks (Task Queue) Includes: setTimeout, setInterval, setImmediate, I/O 👉 Executed after the current script & microtasks 🔹 Microtasks (Microtask Queue) Includes: Promise.then, catch, finally, queueMicrotask 👉 Executed before macrotasks (higher priority) 🧠 Example: console.log("Start"); setTimeout(() => console.log("Timeout"), 0); Promise.resolve().then(() => console.log("Promise")); console.log("End"); 👉 Output: Start End Promise ✅ (microtask runs first) Timeout 💡 Rule: All microtasks run before the next macrotask #JavaScript #EventLoop #Frontend
To view or add a comment, sign in
-
🚀 Day 28 - 💡 JavaScript Tricky Question Explanation Promise.resolve(1) .then(x => x + 1) .then(x => { throw x; }) .catch(x => x + 1) .then(x => console.log(x)); ✨ Output: 3 🧠 Explanation: 1️⃣ Promise.resolve(1) 👉 Starts resolved with value = 1 2️⃣ .then(x => x + 1) 👉 1 + 1 = 2 → passed to next then 3️⃣ .then(x => { throw x; }) 👉 throws 2 ❌ → Promise becomes rejected 👉 Skips remaining then and jumps to catch 4️⃣ .catch(x => x + 1) 👉 receives 2 → returns 3 👉 Promise becomes resolved again 5️⃣ .then(x => console.log(x)) 👉 logs final value → 3 📌 Key Points: - throw inside then = reject - catch handles error and can return new value - after catch, chain continues normally #JavaScript #WebDevelopment #Frontend #Coding #JSConcepts
To view or add a comment, sign in
-
Just built a basic Pac-Man game using JavaScript 🎮 This project helped me practice core concepts like: • Game loops • Collision detection • Keyboard controls • Basic DOM manipulation It’s a simple project, but building even classic games from scratch really strengthens problem-solving and logic skills. Excited to keep building and improving 🚀 #JavaScript #WebDevelopment #LearningByBuilding #Frontend
To view or add a comment, sign in
-
Built a simple Chrome Extension – DownloadEX Hover on any image or video → click ⬇️ → it downloads instantly. Built with: React + TypeScript + Vite + Chrome Extensions (MV3) Learned a lot about: • Content scripts • Background workers • Handling DOM hover properly Still improving it Try it -> https://lnkd.in/gYXprRbR Would love your feedback! #React #JavaScript #ChromeExtension #WebDev
To view or add a comment, sign in
-
🚀 Day 29 - 💡 JavaScript Tricky Question Explanation let a = [1,2,3]; delete a[1]; console.log(a); console.log(a.length); 👉 Output: ``` [1, empty, 3] 3 ``` 👉 Explanation: * `delete` removes the value but does NOT reindex the array * It creates a hole (empty slot) instead of shifting elements * So, `length` remains 3 ⚠️ Use `splice()` if you want to remove and shift elements #JavaScript #WebDevelopment #Frontend #CodingInterview #JSConcepts
To view or add a comment, sign in
-
💻 JavaScript Beginner Level Practice – Count Vowels function countVowels(str){ let vowels = "aeiou"; let count = 0; for(let char of str.toLowerCase()){ if(vowels.includes(char)){ count++; } } return count; } console.log(countVowels("hello world")); 📌 Output: 3 💡 Explanation: This program loops through each character in a string and checks whether it is a vowel (a, e, i, o, u). If it is a vowel, the count increases. #JavaScript #BeginnerLevel #CodingPractice #WebDevelopment
To view or add a comment, sign in
-
-
Everyone got it right… but did you really understand it? 👀 Let’s close Friday’s challenge 👇 👉 console.log(!!"false" == !!"true"); Answer: true ✅ Loved the responses — honestly, most of you nailed it 🔥 But the real win? The ones who explained why 💯 Here’s the catch 👇 JavaScript doesn’t care about the meaning of the string — only whether it’s empty or not. So: "false" → truthy → true "true" → truthy → true 👉 true == true → true Shoutout to everyone who got it right 👏 and extra respect to those who broke it down step-by-step 👀 Next one drops soon — stay ready 🔥 #JavaScript #MERNStack #WebDevelopment #CodingChallenge #Developers #LearnToCode
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