The Wayback Machine - https://web.archive.org/web/20240118182821/https://www.geeksforgeeks.org/async-await-function-in-javascript/
Open In App
Related Articles

Async/Await Function in JavaScript

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

JavaScript is Synchronous in nature which means that it has an event loop that allows you to queue up an action that won’t take place until the loop is available sometime after the code that queued the action has finished executing. But there are a lot of functionalities in our program that make our code Asynchronous and one of them is the Async/Await functionality.

Async/Await is the extension of promises that we get as support in the language.

What is Async Function ?

Async simply allows us to write promises-based code as if it was synchronous and it checks that we are not breaking the execution thread.

Async functions will always return a value. It makes sure that a promise is returned and if it is not returned then JavaScript automatically wraps it in a promise which is resolved with its value.

Example 1: In this example, we will see the basic use of async in JavaScript.

javascript




const getData = async () => {
    let data = "Hello World";
    return data;
}
 
getData().then(data => console.log(data));


Output

Hello World

What is Await Function ?

Await function is used to wait for the promise. It could be used within the async block only.

It makes the code wait until the promise returns a result.

Example 2: This example shows the basic use of the await keyword in JavaScript.

javascript




const getData = async () => {
    let y = await "Hello World";
    console.log(y);
}
 
console.log(1);
getData();
console.log(2);


Output

1
2
Hello World

Notice that the console prints 2 before the “Hello World”. This is due to the usage of the await keyword. 

The async keyword transforms a regular JavaScript function into an asynchronous function, causing it to return a Promise.

The await keyword is used inside an async function to pause its execution and wait for a Promise to resolve before continuing.

Example 3: In this example, we will be implementing several promises in a method, and then that method we will use for displaying our result.

Javascript




function asynchronous_operational_method() {
    let first_promise =
        new Promise((resolve, reject) => resolve("Hello"));
    let second_promise =
        new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(" GeeksforGeeks..");
        }, 1000);
    });
    let combined_promise =
        Promise.all([first_promise, second_promise]);
    return combined_promise;
}
 
async function display() {
    let data = await asynchronous_operational_method();
    console.log(data);
}
 
display();


Output:

[ 'Hello', ' GeeksforGeeks..' ]

Supported Browsers:

The browsers supported by Async/Await Function are listed below:

  • Google Chrome 55 and above
  • Firefox 52 and above
  • Apple Safari 10.1 and above
  • Opera 42 and above
  • Edge 14 and above

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 24 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads
Complete Tutorials