Understanding Promise, async, await (Asynchronous)

Jaeung Kim (Jay)
1 min readFeb 7, 2023

INTRODUCITION

A promise is a good way to handle asynchronous operations. It is used to find out if the asynchronous operation is successfully completed or not.

Prior to promises events and callback functions were used but they had limited functionalities and created unmanageable code. Multiple callback functions would create a callback hell that leads to unmanageable code. Events were not good at handling asynchronous operations.

In other words, Promise makes JavaScript behave asynchronously, which makes this language versatile.

JavaScript engine executes the program synchronously, one line at a time from top to bottom in the exact order of the statements.

Now, let’s take a look at this example

console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);

This will output the following result.

1
2
3
4
5

This seems way too obvious at first, but let’s see how this turns out when implemented with functions.

function a() {
console.log("a called!");
}
console.log(1);
console.log(2);
a();
console.log(3);
console.log(4);

If we execute this, it will return

1
2
a called!
3
4

Although, console.log(“a called!”) exists at the very top, we can see that when executed, it exists in between 2 and 3.

Now,

REFERENCE

  1. https://springfall.cc/post/7

--

--