Synchronous vs asynchronous

Yasuhiro Ito

Programming

Architecture

Wed Sep 15 2021

You are not sure the differences between synchronous and asynchronous? If you are beginner, no worries. I am going to explain as simple as I can.

Synchronous

The basic concept of synchronous is that a program waits for the processing of a code. For example, if the code takes 10 seconds, the program will not do anything until the code finished.

Let's see the below.

const a = 10

this_takes_10sec_to_process_something(a)  // This code 10 seconds

console.log("Hello world!!")

console.log("Hello world!!") will be executed after 10 seconds because this_takes_10sec_to_process_something takes 10 seconds.

The advantage of synchronous

If you write program synchronously, it could be simpler than asynchronous.

The disadvantage of synchronous

In some cases, synchronous can not handle. For example, if you write all Javascript code for web application synchronously, your application might hangup a lot of times. Browsers run your script on a single thread, so you should consider asynchronous.

Asynchronous

The basic concept of asynchronous is that a program does not wait for the processing of a code. For example, even if the code takes 10 seconds to finish, the program does not wait for it.

const a = 10

this_takes_10_seconds_but_does_not_wait_to_process(a)

console.log("Hello World!!")

In this example, console.log("Hello World!!") will be executed immediately. It will not wait 10 seconds.

The advantage of asynchronous

If your application or system does not want to block something or can not wait the result, asynchronous is a good option for it. If you are considering micro-service architecture, you must understand the concept of asynchronous.