DEV Community

Discussion on: JS illustrated: The event loop 🔁

Collapse
 
amineamami profile image
amineamami

Great article, but one question how’s this asynchronous ?

Collapse
 
kapantzak profile image
John Kapantzakis

Thanks @amineamami !
According to MDN: Asynchronous , asynchronous code may refer to multiple related tasks happening without waiting for each other to complete.

In our case, if the code was not asynchronous, the setTimeout call whould have blocked the execution of the program until it was completed printing "Two" at the console.

But this is not the case. The program continues executing, printing "three" and, after that, the callback of the setTimeout is called, printing "Two" at the end.

Collapse
 
amineamami profile image
amineamami • Edited

Hmm, it seemed to me just like synchronous execution with a bit of instructions reordering.

Thread Thread
 
kapantzak profile image
John Kapantzakis • Edited

Thats right! Javascript language itself does not have any asynchrony built in.
The environment (browser, node etc) is the one that arranges the events resulting in asynchronous execution (the use o callbacks, promises, ajax calls etc)