event-handlingdom-eventsv8spidermonkeyjavascript-engine

Javascript internals: how events are implemented?


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?


Solution

  • 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.