How to use JavaScript Await Keyword? [SOLVED]


Written by - Olorunfemi Akinlua
Reviewed by - Deepak Prasad

Introduction to JavaScript Await

The await keyword is a feature of the ECMAScript 2017 (ES8) standard, and it is used to pause the execution of an asynchronous function until a promise is fulfilled. The await keyword is used in conjunction with the async keyword, and it allows you to write asynchronous code in a synchronous-like style, making it easier to read and understand.

In this article, we will explain the await keyword within the asynchronous operations of JavaScript.

 

Using the await keyword in JavaScript

Here is an example of using the await keyword to wait for a promise to be fulfilled. The example function creates a new promise that will be fulfilled in 2 seconds. The await keyword is used to wait for the promise to be fulfilled, and the resulting value is stored in the result variable. The console.log call at the end of the function is executed once the promise has been fulfilled and the result variable has been set.

async function example() {
    // Create a promise that will be fulfilled in 2 seconds
    var myPromise = new Promise((resolve, reject) => {
        setTimeout(() => {
            // Fulfill the promise with the string "hello world"
            resolve("hello world");
        }, 2000);
    });

    // Wait for the promise to be fulfilled
    var result = await myPromise;
    // Do something with the result
    console.log(result);
}

// Call the function to see the output
example();

When you call the example function, you should see the following output after 2 seconds:

hello world

It is important to note that the await keyword can only be used inside of an async function. If you try to use it outside of an async function, you will get a syntax error.

Here is another example that shows how to use the await keyword to wait for multiple promises to be fulfilled:

async function example() {
    let promise1 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("hello");
        }, 2000);
    });

    let promise2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("world");
        }, 2000);
    });

    let result1 = await promise1;
    let result2 = await promise2;

    console.log(result1 + " " + result2);
}

example();

When you call the example function, you should see the following output after 2 seconds:

In this example, the example function creates two promises that will be fulfilled in 2 seconds. The await keyword is used to wait for both promises to be fulfilled, and the resulting values are stored in the result1 and result2 variables. The values are then concatenated and logged to the console.

hello world

 

Summary

The await keyword is a feature of the ECMAScript 2017 (ES8) standard that is used to pause the execution of an asynchronous function until a promise is fulfilled. The "await" keyword is used in conjunction with the "async" keyword, and it allows you to write asynchronous code in a synchronous-like style. The "await" keyword can also be used to handle rejected promises, making it a useful tool for working with asynchronous code in JavaScript.

 

References

await - JavaScript | MDN (mozilla.org)

 

Views: 4

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on LinkedIn.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment