19. Asynchronous Callbacks #

Created Wednesday 2 March 2022

Asynchronous - JS engine vs Browser #

“JavaScript is synchrounous and single threaded” - this statement seems naive or wrong. But it’s not.

“JavaScript” here refers to the working of the JavaScript engine, not the whole browser. The JavaScript engine talks to the rendering engine for making changes on a webpage and talks with the network part of the browser for intermittent data requests.

The JavaScript engine just places requests at these browser APIs, but still runs single threaded and synchronously.

So, the browser as a whole is asynchronous and multi-threaded.

Event loop and queue - the bridge between browser APIs and the JS engine #

The browser is aynchronous and multi-threaded, but the JS engine is not. Well, they cannot just work out of the box. There’s actually a construct inside the JS engine that makes this combination of browser + JS engine, smooth. This construct has two parts:

  1. Event loop
  2. Event queue, which has two subparts:
    1. Microtask queue
    2. Callback queue

How is the event queue populated and depopulated #

The order of code when async elements are involved #

Here’s an example of the order of execution, main code then callbacks and setTimeouts.

function waitForThreeSeconds () {
	let ms = 3000 + (new Date()).getTime();

	while((new Date()) < ms) {}

	console.log('finished function')
}

function clickHandler() {
	console.log('click event!')
}

document.addEventListener('click', clickHandler);

waitForThreeSeconds();
console.log('finished execution');

Result is: Here, the whole pages kind of freezes for the first 3 seconds, because the main code (i.e. code in stack) is running. Still, click events (if any) are picked up by the browser resources and stored in the event queue. Once the 3 second while loop is done, the stack is still not empty, because it needs to call the remaining main code(the “finished function code” console.log). Only after this is executed does the stack become empty. Once the main stack is empty, the click event’s callbacks are pushed into the stack to execute. This is the order of code.