🚀 Day 25 - Poll answer & Explanation console.log("S"); setTimeout(() => console.log("T"), 0); async function t() { console.log("AS"); await null; console.log("AE"); } t(); console.log("E"); /* ✨ Output: S AS E AE T 🧠 Explanation: 1️⃣ console.log("S") 👉 Runs immediately (synchronous) 2️⃣ setTimeout(..., 0) 👉 Goes to macrotask queue ⏳ (runs later) 3️⃣ t() is called 👉 "AS" runs immediately (sync inside async) 4️⃣ await null 👉 Pauses function ⏸️ 👉 "AE" moves to microtask queue ⚡ 5️⃣ console.log("E") 👉 Runs next (still synchronous) 6️⃣ Microtask queue runs ⚡ 👉 "AE" 7️⃣ Macrotask queue runs ⏳ 👉 "T" 📌 Final Order: Synchronous → Microtask → Macrotask */ #JavaScript #JS #WebDevelopment #Frontend #Programming #Coding #CodeChallenge #Tech #Developers #LearnJavaScript #CodingTips #DevCommunity #100DaysOfCode #CodeNewbie #SoftwareDevelopment #TechTrends #InterviewPrep #DailyCoding
JavaScript Execution Order: Synchronous, Microtask, Macrotask
More Relevant Posts
-
🔥 Let’s talk about something we all “know”… but rarely truly understand: The JavaScript Event Loop. Quick question 👇 Have you ever written async code… but your app still felt blocked? 👉 Here’s why: JavaScript runs on a single thread. So if you do this: while(true) {} 💥 Everything stops: UI freezes Promises don’t resolve API calls get delayed 💡 The reality: Async helps with I/O… not CPU work. ⚡ What changed my thinking: “If the main thread is busy, nothing else matters.” 👉 What I do now: ✔ Break heavy tasks into chunks ✔ Avoid long synchronous loops ✔ Use workers when needed Once you truly understand the event loop… debugging becomes 10x easier. What was your biggest “event loop moment”? 😄 #javascript #eventloop #webdevelopment #performance #programming #frontend #backend #softwareengineering #Coding #TechCareers
To view or add a comment, sign in
-
-
avaScript isn’t slow with async… you’re just misunderstanding the Event Loop. JS is single-threaded, but it never “waits.” Async work gets offloaded → queues → then executed smartly. The key insight from this infographic: Microtasks (Promises, async/await) ALWAYS run before callbacks like setTimeout. That’s why this surprises many devs: “Promise” logs before “setTimeout” — even with 0ms delay. Once you understand: Call Stack → Web APIs → Microtask Queue → Callback Queue Everything clicks. If async ever felt confusing, this mental model fixes it. Which part confused you the most before this? #javascript #webdevelopment #frontend #reactjs #programming #async #eventloop #softwareengineering
To view or add a comment, sign in
-
-
🚀 6 React Hooks that changed how I write code — and will change yours too. If you're still confused about when to use what, here's the simplest breakdown: 🔵 useState → Store & update values. Every re-render starts here. 🌐 useEffect → Talk to the outside world (APIs, DOM, subscriptions). 📦 useRef → Hold a value WITHOUT triggering a re-render. A hidden drawer for your data. 🧠 useCallback → Memoize functions so they don't get recreated on every render. ⚡ useMemo → Cache expensive calculations. Only recompute when dependencies change. 🌍 useContext → Share state globally. No more prop drilling through 5 layers. The moment these clicked for me, my components became cleaner, faster, and way easier to debug. Which hook took you the longest to truly understand? Drop it in the comments 👇 #ReactJS #WebDevelopment #JavaScript #Frontend #Programming #React #SoftwareEngineering #100DaysOfCode #CodeNewbie #TechEducation #FrontendDeveloper #ReactHooks
To view or add a comment, sign in
-
-
⚡ Why doesn’t setTimeout(fn, 0) run immediately? Most developers think JavaScript executes things in order… but that’s not always true. Let’s break it Example: console.log("Start"); setTimeout(() => console.log("Timeout"), 0); Promise.resolve().then(() => console.log("Promise")); console.log("End"); Output: Start End Promise Timeout What’s happening? JavaScript uses something called the Event Loop to handle async operations. Here’s the flow: Code runs in the Call Stack Async tasks go to Web APIs Completed tasks move to queues Event Loop pushes them back when stack is empty The twist: Microtasks (HIGH PRIORITY) • Promise.then() • queueMicrotask() Macrotasks (LOWER PRIORITY) • setTimeout() • setInterval() That’s why: Promise executes BEFORE setTimeout — even with 0ms delay Real takeaway: Understanding this can help you debug tricky async issues, optimize performance, and write better code. Have you ever faced a bug because of async behavior? #JavaScript #WebDevelopment #Frontend #Programming #Coding #Developers #100DaysOfCode
To view or add a comment, sign in
-
Writing code is easy. Writing non-blocking, high-performance code is an art. 🎨 The JavaScript Event Loop is the engine under the hood that makes it all possible. As a Full Stack Developer, I prioritize understanding these execution phases to ensure: ✅ Smooth UI rendering by not blocking the Call Stack. ✅ Efficient API handling using the Microtask Queue. ✅ Scalable architecture that respects the browser’s processing limits. When you understand the loop, you stop guessing and start architecting. 🏗️ #FullStackDeveloper #PerformanceOptimization #ProgrammingTips #TechCommunity #JS
To view or add a comment, sign in
-
-
🚨 “setTimeout(cb, 0) runs immediately.” Still believe that? 👀 Then this will break your mental model of JavaScript. I built a visual Event Loop simulator to show what actually happens behind the scenes: 👉 Watch your code move through: • Call Stack • Microtask Queue • Macrotask Queue All in real time. 🎮 3 Levels. Increasing pain. 🔥 Level 1 — The classic trap (you’ll feel confident) ⚡ Level 2 — Promise inception (this is where most devs fall) 🧠 Level 3 — The boss fight (everything combined) 💥 Truth? Almost nobody gets Level 2 right on the first try. 👇 I’m curious — where did you break? Comment: EASY → if you cleared all 3 LEVEL 2 → if that’s where it got tricky 🤯 → if this changed how you think about async JS 🎯 If you’re preparing for interviews or working with async code daily — this is a must-know. ♻️ Repost for your team 💾 Save it for later #javascript #webdev #frontend #eventloop #programming #codinginterview
To view or add a comment, sign in
-
I used to write this to get a user's city from an API response: user && user.address && user.address.city Then I discovered two operators that changed everything. 😅 → ?. optional chaining — if it's null, just return undefined. No crash. → ?? nullish coalescing — if it's null or undefined, use this default instead. → ?? is smarter than || — it won't replace 0 or "" with your default. Huge difference. → Combine them: user?.address?.city ?? "Unknown" — safe access + fallback in one line. → Works on functions too — user.getName?.() only calls it if it exists. Once you start using these — you'll wonder how you lived without them. 😄 Were you using && for this before? Drop a comment 👇 #javascript #webdevelopment #frontend #programming #javascripttips #learnjavascript #100daysofcode #reactjs #coding #softwareengineering
To view or add a comment, sign in
-
setTimeout(fn, 0) looks simple. Until a Promise shows up… and breaks your expectations. You write this: setTimeout(() => console.log("timeout"), 0); Promise.resolve().then(() => console.log("promise")); And you think: “Timeout has 0 delay… it should run first.” But the output is: ->promise ->timeout So what’s actually happening? Not magic. Not randomness. Just how JavaScript is designed. When your code runs, this is the order: Run all synchronous code Execute all Promises (microtasks) Then execute setTimeout (macrotasks) Now read that again. 👉 Promises are not faster 👉 They are just scheduled differently Here’s the hidden detail most devs miss: Even if your setTimeout is ready… JavaScript will pause it until every single Promise is finished. So in reality: setTimeout(fn, 0) → “Run me later” Promise.then() → “Run me right after this code” That’s why Promises always win. Not because they’re special… But because the event loop always clears microtasks first. The simple mental model: 👉 sync → promises → timers Once you understand this, you stop guessing async behavior… …and start predicting it. #javascript #webdev #eventloop #programming #promises #settimeout #nodejs #javascript #nestjs #backend #softwareengineering
To view or add a comment, sign in
-
-
🧠 If you're overusing "useEffect", you're probably doing it wrong. This hook is powerful — but also one of the most misused things in React. Here’s what I see often 👇 ❌ Missing dependency array → unexpected behavior ❌ Adding everything to dependencies → infinite loops ❌ Using useEffect for derived state → unnecessary complexity ✅ Use it only for side effects: - API calls - Subscriptions - DOM interactions --- 💡 Rule of thumb: “If it can be calculated during render, don’t use useEffect.” --- 👀 Let’s discuss: 👉 What’s the most confusing part of useEffect for you? 👉 Have you ever debugged an infinite loop issue? 😅 Drop your thoughts 👇 #ReactJS #Frontend #JavaScript #WebDev #Programming
To view or add a comment, sign in
-
-
A small VS Code feature that saves me time every single day. I remember when I first started working with HTML and JSX, I used to do this a lot: Change an opening tag... Then scroll to find and update the closing tag. Sometimes I'd break things without realizing immediately. Then I discovered Auto Rename Tag. Now, once I edit the opening tag, the closing tag updates automatically. Simple, but very useful. It's one of those tiny improvements that makes development feel smoother over time. PS: What's one VS Code feature or extension you can't code without? #SoftwareDevelopment #VSCode #WebDevelopment #Developers #TechTools
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