Async/await

Vignesh S
Vignesh S
May 3 · 3 min read
Table of Contents

In promises, the object creation starts the execution of asynchronous functionality. Here await only blocks the code execution within the async function. Code Pen

The async keyword, which you put in front of a function declaration to turn it into an async Function that returns a Promise object.
await
keyword accepted only inside the async functions.

In the below example, the f2 method is not declared with the async keyword which returns a value. The f3 method returns a promise when declared with the async keyword.

await keyword causes async function execution to pause until the promise is settled(fulfilled or rejected).

Case1: Perform without await keyword Code Pen

Perform without await keyword

Case2: Perform with the await keyword Code Pen

Perform with the await keyword

Hope this provides an understanding of the async/await function.

We know how the callback turned into Promises then promise chain and now async/await. If a function using a Promise chain, only the promise chain is asynchronous not the entire function. But in async/await the entire function is asynchronous.

Promise chain approach
Async/await approach

In the awaitWay method, we modified all the promise chains to individual function calls with the await keyword. This means the entire function will wait until the function having the await keyword will get resolved.

When seeing the output here first the value 40 prints and then the statement prints “is this really run in Asynchronous way? Yes ”. It was printed vice versa in Promise chains

Hope it will give a basic understanding of Async/await, Please write your questions for any doubts or corrections.

Whatever we learned as of now from ES features enough to start the ReactJS. Let’s start our next topic about React Basics.

Next topic: Learn React from scratch

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store