I will be trying to avoid using language that makes it difficult to follow the article when paired with a concept that you don't fully understand yet. This is to make it as beginner friendly as possible. Think of this article as more of a starting point to learning more about asynchronicity.
I hope this is helpful to anyone who reads it but I'd also like to add that I am by no means an expert and so if there is anything incorrect, please correct me below 😊
In all programs, it is important to distinguish between what happens now and what happens later.
For example, if you have a website and you load a page of that website. A chunk of code from your program has been executed immediately to get that page to load and show what it needs to show. However, other chunks of code in your program have not been executed immediately as they could be waiting for events (e.g. a mouse click).
So, behaviour that is executed at the point in time in which the function is called synchronous behaviour.
Behaviour that is executed at a later point in time from when the function was called is asynchronous behaviour.
Another example of asynchronous behaviour is when you could be waiting for a response back after a event has occurred and a request has been made. For example:
- when requesting data from a database or file system
- when sending data across the network and waiting for a response
This is a problem for asynchronous actions as this will cause a program to stall and appear frozen whilst it's waiting for the asynchronous action to be fully performed.
Therefore, knowing about and accounting for asynchronous behaviour in your programs can help you make sure you are providing the best experiences for users of your program.
The event loop handles executing chunks of code in your program by invoking the JS engine. It is located in the hosting environment (e.g. a web browser) and you can think of it like a container storing a queue of actions to be performed by the JS engine. The actions at the top of queue get performed first and so on.
So, let's follow an example of the flow of what is happening in an asynchronous event.
Example asynchronous event: When sending data across the network and waiting for a response
- Your JS program makes a fetch request for data to the server.
- Whilst it's waiting for the response back, the JS engine tells the hosting environment to perform a callback function when the response is received.
- When the hosting environment gets the response back from the server, it puts the callback function into the event loop to be performed.
- When the callback function gets to the top of the queue in the event loop, the event loop passes it to the JS engine to be performed.
(Source: this flow was adapted from a description of the event loop by You Don't Know JS - found here)
- Asynchronicity refers to events that happen later in time and is an important concept to be considering when writing programs (in any language!).
- The event loop determines the order in which the JS engine performs JS actions.
I think I will stop here for this article. The next article will deal more with how asynchronous events are handled in JS programs (e.g. callbacks, promises etc...).
Thanks for reading! 😁