Angular Trick Question: JavaScript Event Loop and Microtask Queue

🚨 Angular Trick Question That Even Experienced Devs Fail! Let’s see if you can crack this 👇 ❓ Question: What will be the output of this code? @Component({ selector: 'app-root', template: `{{value}}` }) export class AppComponent { value = 0; ngOnInit() { setTimeout(() => { this.value = 10; }, 0); Promise.resolve().then(() => { this.value = 20; }); } } 👇 Think before you answer… --- 💥 Most common answers: 👉 10 👉 30 👉 Depends on timing ❌ All WRONG (or incomplete) --- 💡 Correct Answer: 20 🔥 Why? Because of JavaScript Event Loop + Microtask Queue 🔹 "Promise.then()" → goes to microtask queue 🔹 "setTimeout()" → goes to macrotask queue 👉 Microtasks ALWAYS execute before macrotasks So execution order: 1. "Promise.then()" → sets value = 20 2. "setTimeout()" → sets value = 10 (but Angular change detection already ran) ⚡ Angular detects change after microtask → UI shows 20 --- 🧠 Real Insight: This is why async bugs in Angular are tricky — it’s not just Angular, it’s how JS works under the hood. 🔥 Comment your answer before reading others 👀 Let’s see who actually understands this deeply #Angular #JavaScript #Frontend #CodingInterview #WebDev #RxJS #AngularTips

To view or add a comment, sign in

Explore content categories