Asynchronous Operation in Programming
Things you have to understand about the asynchronous operation in javascript
An operation that can “do its own thing” without blocking the rest of the program from running. It runs independently of the main program flow. Because it avoids blocking the main program flow it can make a program efficient however it can also make code complex since its results are not available right away.
Example of asynchronous operations includes:
Timers
Timers are a common example of an asynchronous operation. When you use setTimeOut
or setInterval
functions you specify a function to be called after a certain amount of time.
setTimeout(() => {
// this function is called after 1000 milliseconds (1 second)
}, 1000);
User Input
Handling input like keyboard or mouse events is an asynchronous operation. A program has to wait for the event to be triggered before it can handle it. Event handling runs independently of the main program flow, you will not know when will the event be triggered so it is an asynchronous operation. In javascript, the click event is listened to by the DOM so it is an asynchronous function.
document.addEventListener('click', event => {
//…