Javascript Async and Await

Javascript 10 min min read Updated: Mar 09, 2026 Intermediate
Javascript Async and Await
Intermediate Topic 14 of 15

Async and Await are modern JavaScript features introduced in ES8 that simplify working with asynchronous code. They are built on top of Promises and allow developers to write asynchronous code that looks and behaves more like synchronous code.

Before async and await, developers mainly used callbacks or promises to handle asynchronous operations. While promises improved readability, async and await make asynchronous code even easier to understand and maintain.

What is Async and Await?

The async keyword is used to declare an asynchronous function, while the await keyword pauses the execution of that function until a promise is resolved.

Key Point: Async functions always return a Promise, and await can only be used inside async functions.

Using the async Keyword

The async keyword is placed before a function declaration. It tells JavaScript that the function will return a promise.

javascript async function greet(){ return "Hello Student"; } greet().then(function(message){ console.log(message); });
Output

Hello Student

Using the await Keyword

The await keyword pauses the execution of an async function until the promise is resolved. This makes asynchronous code easier to read and understand.

javascript function getData(){ return new Promise(function(resolve){ setTimeout(function(){ resolve("Data loaded"); }, 2000); }); } async function showData(){ let result = await getData(); console.log(result); } showData();
Output

Data loaded

Handling Errors with try and catch

When working with async and await, errors can be handled using the try...catch block.

javascript async function fetchData(){ try{ let response = await Promise.reject("Error occurred"); console.log(response); } catch(error){ console.log(error); } } fetchData();
Output

Error occurred

Key Point: try...catch is commonly used with async and await to handle errors in asynchronous operations.

Advantages of Async and Await

  • Makes asynchronous code easier to read
  • Reduces complexity compared to promise chains
  • Improves error handling using try...catch
  • Helps developers write cleaner code

Conclusion

Async and Await provide a modern and cleaner way to handle asynchronous operations in JavaScript. By simplifying promise-based code, they make programs easier to read and maintain.

These features are widely used in modern JavaScript applications, especially when working with APIs, databases, and other asynchronous tasks.

In the next tutorial, you will learn about the Fetch API in JavaScript, which is commonly used to retrieve data from servers.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators