Skip to content
Related Articles

Related Articles

Async/Await Function in JavaScript

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 02 Jun, 2022
View Discussion
Improve Article
Save Article

We all know that 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’s a lot of functionalities in our program which makes our code Asynchronous and one of them is the Async/Await functionality. Async/Await is the extension of promises which we get as a support in the language. 

You can refer Promises in Javascript to know more about it. 

Following sections will describe more about async and await in detail along with some examples (individual as well as combined examples of async-await):

Async: It simply allows us to write promises based code as if it was synchronous and it checks that we are not breaking the execution thread. It operates asynchronously via the event-loop. 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.



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


Hello World

Await: 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. It only makes the async block wait.



const getData = async() => {
    var y = await "Hello World";


Hello World

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

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.


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();


[ '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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!