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
E
EvokeMe555d ago
A
anonn555d 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?
E
EvokeMe555d ago
There is about sections. U can read about it.
A
anonn555d 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?
E
EvokeMe555d ago
It's just fn (reaction on resolved promise) that microtask. I think in about section pretty good description.
UU
Unknown User555d ago