Can someone explain why, in the code snippet below, line (a) triggers the 'readystatechange' event and logs this.readyState to the console before logging "ends" at line (b)? Shouldn't the callback be placed in the event queue and only executed after the main script finishes?
const somePath = 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange';
const xhr = new XMLHttpRequest();
console.log(xhr.readyState); // This will output 0
xhr.addEventListener('readystatechange', function() {
console.log(this.readyState)
});
xhr.open('GET', somePath, true); // (a) Will output 1 to the console
console.log('ends'); // (b)