My question is related to how the JS engines implement the pattern of asynchronous events when we do something like bind event handlers on a dom for lets say a click event?
Do they have something like a separate thread that is listening to all the click events? When an event does occur, do they refer the bind list and bubble up the events?
Similar is with Ajax,the asynchronous network call, where the browser spans a new thread that would start listening to the data from the server and when the response is received, it would call the success handler?
JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc.
An event handler is an implementation of the EventHandler interface. The handle() method of this interface provides the code that is executed when the event that is associated with the handler is received by the node that registered the handler. To register a handler, use the addEventHandler() method.
Events are signals fired inside the browser window that notify of changes in the browser or operating system environment. Programmers can create event handler code that will run when an event fires, allowing web pages to respond appropriately to change.
Read this post about the javascript event queue and see if it answers most of your question. There will be a native OS thread that handles interfacing with actual OS events (mouse events, keyboard events, timer events, network I/O events, etc...) and those are then fed into the JS queue where the JS engine can further dispatch them to Javascript code. How many separate threads there are at the OS level is implementation specific and likely does vary with the implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With