Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

Async and Await in JavaScript

The async and await keywords make Promise-based asynchronous JavaScript easier to write, read, and understand.

What are async and await?

JavaScript uses asynchronous programming to perform time-consuming tasks without blocking the application. Promises represent the result of an asynchronous operation, while async and await provide a cleaner way to work with those Promises.

Simple Definition

async is used to declare an asynchronous function.

await pauses the execution of an async function until a Promise is completed.

Together, async and await make asynchronous code look more like normal step-by-step code.

How async and await Work
Start
→
async Function
→
await Promise
→
Result

async vs await

async

The async keyword is placed before a function. An async function returns a Promise.

await

The await keyword is used inside an async function to wait for a Promise to complete.

Important: await is normally used inside an async function. It makes Promise-based code easier to understand because the code can be written in a sequential style.

Example 1: async Function

An async function automatically returns a Promise.

async function getMessage() {

    return "Hello from async function";

}

getMessage().then(function(result) {

    console.log(result);

});
Hello from async function

Example 2: await with Promise

The await keyword waits for a Promise to complete before continuing with the next statement.

function getData() {

    return Promise.resolve("Data received");

}

async function loadData() {

    const result = await getData();

    console.log(result);

}

loadData();
Data received

Example 3: async/await with try-catch

async and await can also be used with try-catch for error handling.

function getData() {

    return Promise.resolve("Data loaded successfully");

}

async function loadData() {

    try {

        const result = await getData();

        console.log(result);

    }
    catch (error) {

        console.log("Error:", error);

    }

}

loadData();
Data loaded successfully

Why Use async and await?

Important Benefits

• Makes asynchronous code easier to read.

• Reduces complicated Promise chains.

• Makes asynchronous operations look sequential.

• Works naturally with try-catch error handling.

• Useful for API and network operations.

Example 🌍🤓

In a real application, async and await are commonly used when retrieving data from an API.

API Request Flow
User Action
→
API Request
→
await
→
API Response
Compiler Note: The CIIT JavaScript compiler is console-based. The Try It buttons below use compiler-safe code so that the result is displayed immediately in the compiler output window.

Summary

The async keyword is used to create an asynchronous function, while await is used to wait for a Promise inside that function. Together they make asynchronous JavaScript easier to read and maintain. They are commonly used with Promises, API requests, and error handling with try-catch.