JS: Async/Await with Promise

Fredric Cliver
3 min readJul 8, 2020

If we invoke a call spend much of time, it breaks an expectable sequence.

In this situation, we can use an async/await keyword to wait a function need to spend time

So, first of all, the await keyword has to be added to the front of the method that needs time.

But, This is not enough. await keyword is only working inside of async function.

But, even though both were added, it looks like not enough. Because the await keyword only waits for Promise object.

But, after returned the Promise, the last log doesn’t be printed.
Cause, the Promise needs special two types of return and we need to put them inside of Promise.

If not, the ‘await’ will be never ended.

Then, it works as exactly what we want.

To easy to see, it looked off from the main sequence to separated function.

Additionally, what is the reject?

If the reject has been invoked, it returned an error. and we need to deal with the error by a catch handler.

As this way, we can catch a failure with a specified message.

In this video, you can find a whole story of a way to deal with asynchronous function in the JS.

--

--

Fredric Cliver

Majored in Physics, self-taught and worked in the IT industry as a Dev/Design/Planning for 11 years. And I had run my Startup for 3 years. I fancy a ☔️ 🇬🇧