Difference between synchronous and asynchronous process

Introduction

There are some major differences between promises and observables. They can call callback functions when the operations are done. Promises have only one pipeline. Both handle the asynchronous calls and events only. 
Difference between synchronous and asynchronous events
Synchronous - In this case, one statement should be completed before going to the second statement. It is evaluated or run in the order of the events. It waits for each operation to complete. It will not run the next statement until first completes. If the first statement takes time, then the program will be in a blocked state. Each statement is executed one after the other.
  • Some operations like image processing, database query, handling large data, making requests and waiting for the response could be very slow tasks in execution. It results in "Blocking."
  • It results in delaying of the program and has a direct risk of delay load time.
  • A single delay in any of the operation can lead to delay in load time.
  • Performance degrades due to blocking of the code.
Asynchronous - In this case, one statement will not block the way for other statements. Execution of the program will not be delayed. It will queue the statements and run them in a queue. Our javascript engine has two parts:
  • One part of the engine will take care of the code and enqueues the operations and statements.
  • Another part will work on the queue to process it. The queue processing happens in one thread, that's why one operation only one operation or statement can execute at once.
  • Code is processed parallelly.
  • If one operation taking time, it will not affect the performance of the code. 
  • If one process is taking longer time to finish or execute, it will not sit idle.
  • It will take the next statement or task in the queue.
  • Whenever the time taking process will complete, it can notify the user using some callback function.
  • Async operations save time and less performance degradation.
Real Example
Suppose we went to a restaurant where there are only two waiters. One for taking the order, other for serving. These waiters will perform their dedicated tasks only in any situation. In this case, let's imagine a situation where many guests arrived at once. As they have only one waiter to take the order. That waiter will take order one by one. Until it takes the order of the first guest, second needs to wait. We have one more waiter but we can't proceed as it's synchronous process example. Like this, all the guests will be waiting for the prior guest to place the order. If some guests are taking much time to place an order, in that situation also that waiter can't go on to another guest. He needs to wait for that particular guest for placing an order. The process will continue one by one. The waiting time for guests will increase in such a process. While on the other hand, if we have an asynchronous model of working. The one waiter can take the order on a time basis. If the first guest is taking time to decide the order, the waiter can take the order for the second guest and so on. When the first guest decides, it can call the waiter and then place their order. In this way, the process will not get blocked. It will be an ongoing process. In this case, there are very fewer chances of blocking. No guest needs to wait for a longer time. 

Illustration
The synchronous process will not involve callbacks mostly. Let us try two different programs as defined below:

var a = database.query(Select * from employees); // Synchronous example
console.log(a);
console.log("Hello Employees");

The second one

database.query(Select * from employees, fn(result)){  //Asynchronous example
var a = result;
console.log(a);
}
console.log("Hello Employees");

If we see the above two set of statements. In the first approach, the very first statement is a database query. The console log of employees will not print until the query result is not completed. But in the second case "Hello employees" will print first then the value of variable a.

The first approach result will be
a
Hello Employees

But the second approach
Hello Employees
a

Comments