When your setTimeout dreams are crushed by VIP Promises.

Imagine this:
You’re an aspiring actor standing outside a big Bollywood studio with your script in hand (ahem, that’s your setTimeout).
You’ve been waiting in line for hours.
Just when you're about to get in, someone whispers:
“Sir, one of the Kapoor’s nephews is here.”
Boom. He walks right in. You? You keep waiting.
There are two types of queues here:
Microtask Queue: where the VIPs chill — .then(), .catch(), .finally() — all the star kids.
Task Queue: the commoners’ line — setTimeout, setInterval, fetch, etc.
And the Event Loop? He’s the casting director.
Super biased!
He always checks the Microtask Queue first.
If there’s anything there, he runs it. And then checks again.
And again.
And again.
Let’s say a Promise resolves.
Cool. But now it says:
“Bro, my cousin Promise wants to act too.”
So inside .then(), you chain another .then(), and another…
Now it becomes a never-ending referral chain.
A full-blown nepotism loop.
Meanwhile, your setTimeout is still outside the studio door like:
“Bhai, mera number kab aayega?”
And the director’s like:
“Bas abhi microtask khatam karta hoon…”
But they never end. 😭
Because microtasks keep adding more microtasks, the Task Queue never even gets looked at.
And that’s literally what starvation means! When the low-priority tasks don’t get a chance to run, because high-priority ones keep hogging the CPU.
This happens more often than you think.
Promise.resolve().then(function first() {
console.log("first");
Promise.resolve().then(function second() {
console.log("second");
Promise.resolve().then(function third() {
console.log("third");
});
});
});
setTimeout(() => {
console.log("Timeout finally ran!");
}, 0);Looks like setTimeout should run immediately, right?
But nope. The nested promises keep pushing each other ahead, because… nepotism.
Run this on jsv9000.app and see the execution order yourself. It’s super satisfying!
Microtasks are like ladies in a queue 🤪 always getting priority because the Event Loop is too polite.
Be careful with how deep you go with nested Promises.
Basically don’t let your app be a Bollywood nepotism story.
Let the outsiders shine too ✨
If you liked this fun explanation, let me know what concept I should break down next ! 🤗
6
9
0