I'm trying to wrap my brain around the job queue and resolving promises in Javascript. I use the book of Nicholas Zakas "Understanding ECMAScript 6", and there is this fragment of code about Promice.race() with description, which I bolded, that i don't understand:
let p1 = new Promise(function(resolve, reject) {
resolve(42);
});
let p2 = Promise.reject(43);
let p3 = new Promise(function(resolve, reject) {
resolve(44);
});
let p4 = Promise.race([p1, p2, p3]);
p4.catch(function(value) {
console.log(value); // 43
});
Here, p4 is rejected because p2 is already in the rejected state when Promise.race() is called. Even though p1 and p3 are fulfilled, those results are ignored because they occur after p2 is rejected.
I know that Promise.race() settles as soon as first of all promises provided is settled. But why does the book claim promise p1 settles after p2? If the code goes from the top to the bottom, the p1 occurs first, so I assume it should settle first. Is it because of the wrapping in function? Or javascript executing order that I don't understand?
Could someone clarify that concept for me?