anonn
anonn2y ago

anonn – 15-18 Oct 10

Hi, I have trouble understanding how promises work in terms of the event loop.
function a() {
console.log('a')
}

function b() {
console.log("b");
}

function c() {
console.log("c")
}

fetch("https://fakelinkabcabc.com").catch(a);

Promise.resolve().then(b);

Promise.reject().catch(c);
function a() {
console.log('a')
}

function b() {
console.log("b");
}

function c() {
console.log("c")
}

fetch("https://fakelinkabcabc.com").catch(a);

Promise.resolve().then(b);

Promise.reject().catch(c);
Why is the output b,c,a Could someone explain in terms of the event loop? e.g the image attached What actually happens when the javascript engine reaches Promise.resolve()? What actually happens when the .then(b) is reached? I have been unable to find resources that go through step by step how interacting with Promises/Web APIs work in terms of the event loop and would be very happy if someone could point me some resources
7 Replies
anonn
anonn2y ago
That is actually where I got the example, but I am still confused. So step 1, b gets put into the microtask queue. How does it get there? What does Promise.resolve() do in terms of the event loop? What does .then() do in terms of the event loop? Who puts b into the microtask queue? Is it the Web API?
EvokeMe
EvokeMe2y ago
There is about sections. U can read about it.
anonn
anonn2y ago
I am confused by
Promise.resolve()
.then(() => console.log('hi'));
Promise.resolve()
.then(() => console.log('hi'));
So the javascript engine sees Promise.resolve() and returns a fulfilled promise. We go to the next line, .then(() => console.log('hi'));. What is the step by step process that happens now? e.g does the javascript engine see that the attached promise is resolved then throw the arrow function to the microtask queue?
EvokeMe
EvokeMe2y ago
It's just fn (reaction on resolved promise) that microtask. I think in about section pretty good description.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1029050226176184320