Excerpt from the book "Secrets of a Javascript Ninja" :
EVENTS ARE ASYNCHRONOUS
Events, when they happen, can occur at unpredictable times and in an unpredictable order (it’s tricky to force users to press keys or click in some particular order). We say that the handling of events, and therefore the invocation of their handling functions, is asynchronous.
The following types of events can occur, among others:
■ Browser events, such as when a page is finished loading or when it’s to be unloaded
■ Network events, such as responses coming from the server (Ajax events, serverside events)
■ User events, such as mouse clicks, mouse moves, and key presses
■ Timer events, such as when a timeout expires or an interval fires
Are all these above events handled by same queue in the event loop? I got this doubt while trying to test a DOM click event in a script :
<html>
<body>
<button id="bId">A button</button>
</body>
<script>
function testing() {
document.getElementById('bId').addEventListener("click", function () {
console.log('clicked');
})
console.log('before click');
document.getElementById('bId').click();
console.log('after click')
};
testing();
</script>
</html>
As i understand, any asynchronous events (like timer, promises) using the event loop queue will wait for call stack to be empty.
In the above code if the DOM events were using the event loop queue shouldn't the output clicked be printed after the before click and after click console.log() function calls?
Are there separate types of queues for different kinds of events?
setTimeoutandsetIntervalwait for call stack to be free? Not even the callbacks or promises?