Skip to content

Latest commit

 

History

History
75 lines (55 loc) · 3.21 KB

module-1.2-promises-and-async-await.md

File metadata and controls

75 lines (55 loc) · 3.21 KB

Module 1: Advanced JavaScript Concepts

1.2. Promises and Async/Await

Promises and the async/await syntax are critical concepts in modern JavaScript for managing asynchronous operations. They simplify asynchronous code and make it more readable and maintainable. Let's explore these concepts in depth with examples:

Promises:

Promises are objects that represent the eventual completion or failure of an asynchronous operation. They provide a way to work with asynchronous code in a more structured and organized manner.

A promise can be in one of three states:

  1. Pending: The initial state, representing that the operation has not yet completed.
  2. Fulfilled: The state when the operation has succeeded, and a result is available.
  3. Rejected: The state when the operation has failed, and an error reason is available.

Example: Using Promises

function asyncOperation() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const result = Math.random();
      if (result >= 0.5) {
        resolve(result); // Resolve with a result
      } else {
        reject("Operation failed"); // Reject with an error
      }
    }, 1000);
  });
}

asyncOperation()
  .then(result => {
    console.log("Operation succeeded with result:", result);
  })
  .catch(error => {
    console.error("Operation failed with error:", error);
  });

In the example above, we create a promise that simulates an asynchronous operation and resolves or rejects based on a random number. We then use .then() to handle success and .catch() to handle errors.

Async/Await:

The async/await syntax provides a more readable and synchronous-like way to work with promises. The async keyword is used to declare an asynchronous function, and the await keyword is used inside such functions to pause the execution until a promise is resolved.

Example: Using Async/Await

async function performAsyncTask() {
  try {
    const result = await asyncOperation();
    console.log("Operation succeeded with result:", result);
  } catch (error) {
    console.error("Operation failed with error:", error);
  }
}

performAsyncTask();

In this example, we declare an async function performAsyncTask, which uses await to pause until the asyncOperation promise is resolved or rejected. This makes the code look more synchronous and easier to read.

Practical Uses:

Promises and async/await are widely used in JavaScript for various asynchronous tasks, including:

  1. HTTP Requests: Promises are often used to manage AJAX requests, making it easier to handle responses.

  2. Timeouts and Intervals: Promises are useful for scheduling tasks to run after a specified time interval.

  3. File I/O: Promises simplify reading and writing files in Node.js applications.

  4. Database Queries: Promises make it easier to work with databases and manage queries.

  5. Parallel and Sequential Execution: Async/await allows you to control the flow of asynchronous operations, whether they need to run in parallel or sequentially.

Promises and async/await have become integral tools for handling asynchronous operations in JavaScript, improving code readability and maintainability while reducing callback hell.