Working with Asynchronous Node js
Read This on Github
⭐ What is Synchronous ?
Explain
Synchronous with an example
Synchronous: Tasks run one after another, waiting for each to finish before starting the next. Example: Washing clothes, then drying, then ironing. Each task waits for the previous to finish.
Suppose I have three functions. Function A , Function B , Function C.
In this case,
- Function Aruns and completes- Function Bruns after- Function Acompletes and completes- Function Cruns after- Function Bcompletes and completes
 
 
The next function doesn't start until the previous one has completed. In other words, the functions are executed in a sequential order.
⭐ What is Asynchronous ?
Explain
Asynchronous with an example
Asynchronous: Tasks can run simultaneously, not waiting for others to finish. Example: Washing clothes, while another set is drying, and another is being ironed. Tasks happen independently without waiting.
Suppose I have three functions. Function A , Function B , Function C.
In this case,
- Function Aruns and starts in the background
- Function Bruns and starts in the background
- Function Cruns and starts in the background- Function Acompletes
- Function Bcompletes
- Function Ccompletes
 
The next function doesn't have to wait for the previous one to complete before starting. In other words, the functions are executed concurrently.
⭐ Synchronous Basic Code
Let's see an basic example of Synchronous
 
console.log("Function 1");
console.log("Function 2");
console.log("Function 3"); 
   Function 1
   Function 2
   Function 3In this example, the program will execute the functions one after the other in order. So it will log "Function 1", then "Function 2", and then "Function 3".
This is because synchronous execution means that the program will wait for a function to finish before moving on to the next one.
⭐ Asynchronous Basic Code
Let's see an basic example of Asynchronous
 
console.log("Function 1");
setTimeout(() => {
console.log("Function 2");
}, 2000);
console.log("Function 3"); 
   Function 1
   Function 3
   Function 2In this example,
- The code starts by logging "Function 1" to the console.
- The setTimeout function is called, which delays the execution of its callback function by 2000 milliseconds (2 seconds).
- While the setTimeout function is waiting for the delay to complete, the program moves on to the next line of code, which logs "Function 3" to the console.
- After 2 seconds, the callback function inside the setTimeout function is executed, logging "Function 2" to the console.
So even though the setTimeout function is called before "Function 3" is logged, it doesn't block the program from executing the next line of code, resulting in "Function 3" being logged before "Function 2".
⭐ Asynchronous More Examples
Let's see more examples of Asynchronous
 
let x=10;
let y=0;
 
setTimeout(() => {
 y = 20;
}, 2000);
 
console.log(x+y); 10Instead of 30 we get 10. This is because the setTimeout function is called after the console.log function, so the program will log 10(x+y where y=0) to the console before the setTimeout function is executed, which sets y to 20.
setTimeout is a function that is built into the browser. It takes two arguments: a callback function and a delay in milliseconds. The callback function is executed after the delay has passed.
In Synchronous operations tasks are performed one at a time, In Asynchronous, Second Task do not wait to finish First Task
