Open In App

How to use await outside of an async function in JavaScript ?

In this article, we will try to understand in what way or by how we may use await outside of an async function in JavaScript with the help of both theoretical explanations as well as coding examples.

Let us first understand the following shown section in which all the syntaxes of declaring a promise, executing a promise, and using async-await keywords, are enlightened in a brief manner:



Syntax: The following shown syntax is getting in use in order to declare a promise in JavaScript:

let new_promise_variable = 
    new Promise ((resolve, reject) => {
        // either use resolve()
        // or use reject() method
    });

The following shown syntax will help us to understand how we to execute a declared promise in JavaScript:



new_promise_variable.then((data) => {
    // Either use console.log() for data
    // or use return statement
}).catch((error) => {
    // Catch the error, if caught
})

Another shown syntax will help us to understand how to use async-await keywords for declaring an async function followed by fetching the data properly using await-keyword:

async function function_name () {
    let data_to_be_retrieved = await data_function();
    // Do something with the fetched data
}

Now after analyzing all the above-shown syntaxes (shown above), let us have a look over the below-enlightened examples which will help us to understand the syntaxes properly.

Example 1: 




<script>
    let getData = () => {
        return new Promise((resolve, reject) => {
            resolve("This article is available "
                + "on GeeksforGeeks...");
        });
    };
  
    let data = getData();
    data
        .then((result) => {
            console.log(result);
        })
        .catch((error) => {
            console.log(error);
        });
</script>

Output:

This article is available on GeeksforGeeks...

Example 2: 




<script>
    let getData = () => {
        return new Promise((resolve, reject) => {
            resolve("This article is available"
                + " on GeeksforGeeks...");
        });
    };
  
    let fetchData = async () => {
        try {
            let data = await getData();
            console.log(data);
        } catch (error) {
            console.log(error);
        }
    };
  
    fetchData();
</script>

Output:

This article is available on GeeksforGeeks...

Now that we have understood things completely in a very deep and detailed manner, let us now look over the below-enlightened example which will help us to understand our main task which is how we could use await outside of the async-function. 

Example 3: 




<script>
    let getData = () => {
        return new Promise((resolve, reject) => {
            reject("Something went wrong!!...");
        });
    };
  
    (async () => {
        try {
            let data = await getData();
            console.log(data);
        } catch (error) {
            console.log(error);
        }
    })();
</script>

Output:

Something went wrong!!...

Article Tags :