Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Async/Await Syntax in JavaScript


In JavaScript, asynchronous programming can be handled in several ways, such as using callbacks or promises. However, with the introduction of async and await in ECMAScript 2017 (ES8), handling asynchronous code has become much more readable and easier to manage. In this article, we will explore the async and await syntax in JavaScript, with examples to demonstrate how it works.

1. What is Async/Await?

async and await are used to handle asynchronous operations in a more synchronous-looking manner, making the code easier to read and understand. The async keyword is used to declare an asynchronous function, and the await keyword is used inside the async function to pause the execution until the promise is resolved or rejected.

2. Declaring an Async Function

An async function always returns a promise. Even if the function does not explicitly return a promise, it will return one by default. Here’s an example of a simple async function:

          async function myAsyncFunction() {
              return "Hello, World!";
          }

          myAsyncFunction().then(function(result) {
              console.log(result);  // Output: Hello, World!
          });
      

In this example, myAsyncFunction is an async function that returns a string. The result is automatically wrapped in a resolved promise, and we use .then() to handle the resolved value.

3. Using Await Inside an Async Function

The await keyword is used to wait for a promise to resolve inside an async function. The execution of the function is paused until the promise is either resolved or rejected. Here’s an example:

          async function fetchData() {
              let result = await new Promise(function(resolve, reject) {
                  setTimeout(function() {
                      resolve("Data fetched successfully!");
                  }, 2000);
              });
              console.log(result);  // Output after 2 seconds: Data fetched successfully!
          }

          fetchData();
      

In this example, fetchData is an async function that waits for the promise inside it to resolve before logging the result. The setTimeout function simulates an asynchronous task, such as fetching data from a server.

4. Handling Errors with Try/Catch

When working with asynchronous code, it’s important to handle errors. With async/await, errors can be caught using the try/catch statement. This makes error handling easier compared to using .catch() with promises.

          async function fetchData(success) {
              try {
                  let result = await new Promise(function(resolve, reject) {
                      setTimeout(function() {
                          if (success) {
                              resolve("Data fetched successfully!");
                          } else {
                              reject("Error fetching data.");
                          }
                      }, 2000);
                  });
                  console.log(result);  // Output if success: Data fetched successfully!
              } catch (error) {
                  console.log(error);  // Output if failed: Error fetching data.
              }
          }

          fetchData(true);  // This will resolve
          fetchData(false);  // This will reject
      

In this example, if the promise is rejected, the catch block will handle the error, making it easier to manage failures in asynchronous code.

5. Example: Chaining Multiple Async/Await Operations

With async/await, you can chain multiple asynchronous operations in a clean and readable way. The function will wait for one operation to finish before proceeding to the next one.

          async function task1() {
              return new Promise(function(resolve) {
                  setTimeout(function() {
                      console.log("Task 1 completed.");
                      resolve();
                  }, 1000);
              });
          }

          async function task2() {
              return new Promise(function(resolve) {
                  setTimeout(function() {
                      console.log("Task 2 completed.");
                      resolve();
                  }, 1000);
              });
          }

          async function task3() {
              return new Promise(function(resolve) {
                  setTimeout(function() {
                      console.log("Task 3 completed.");
                      resolve();
                  }, 1000);
              });
          }

          async function executeTasks() {
              await task1();
              await task2();
              await task3();
              console.log("All tasks completed.");
          }

          executeTasks();
          // Output:
          // Task 1 completed.
          // Task 2 completed.
          // Task 3 completed.
          // All tasks completed.
      

In this example, executeTasks is an async function that waits for each task to complete before moving on to the next one. This avoids the need for nested then() calls, making the code much more readable.

6. Working with Multiple Promises Concurrently

If you want to run multiple asynchronous operations concurrently but still wait for all of them to complete, you can use Promise.all() in combination with async/await.

          async function task1() {
              return new Promise(function(resolve) {
                  setTimeout(function() {
                      console.log("Task 1 completed.");
                      resolve("Task 1 result");
                  }, 1000);
              });
          }

          async function task2() {
              return new Promise(function(resolve) {
                  setTimeout(function() {
                      console.log("Task 2 completed.");
                      resolve("Task 2 result");
                  }, 1500);
              });
          }

          async function task3() {
              return new Promise(function(resolve) {
                  setTimeout(function() {
                      console.log("Task 3 completed.");
                      resolve("Task 3 result");
                  }, 500);
              });
          }

          async function executeTasks() {
              let results = await Promise.all([task1(), task2(), task3()]);
              console.log("All tasks completed:", results);
          }

          executeTasks();
          // Output:
          // Task 1 completed.
          // Task 2 completed.
          // Task 3 completed.
          // All tasks completed: ["Task 1 result", "Task 2 result", "Task 3 result"]
      

In this example, the three tasks are executed concurrently. Promise.all() is used to wait for all of them to complete before proceeding, and the results of all tasks are returned in an array.

7. Conclusion

The async/await syntax in JavaScript provides a powerful way to handle asynchronous operations in a more readable and maintainable way. By using async to declare a function and await to pause the execution until a promise is resolved, you can write cleaner code that is easier to understand and debug. The try/catch block allows for more efficient error handling, making asynchronous code much easier to manage compared to traditional callbacks or promise chaining.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java