Skip to content

Latest commit

 

History

History
185 lines (129 loc) · 5.53 KB

Purpose-of-async-and-await.md

File metadata and controls

185 lines (129 loc) · 5.53 KB

What is the Purpose of the async and await Keywords ?

The async and await keywords in JavaScript are used to handle asynchronous operations in a more readable and concise way, making the code easier to understand and maintain. They are built on top of Promises and provide a cleaner syntax for working with asynchronous tasks, such as API calls or timers.


What is async?

The async keyword is used to declare a function as asynchronous. When a function is marked as async, it always returns a Promise, even if you don’t explicitly return one. Within an async function, you can use the await keyword to pause the execution until a Promise is resolved or rejected.

Key Points:

  1. Declares a function as asynchronous.
  2. Ensures the function always returns a Promise.
  3. Allows the use of await within its body.

Example:

async function fetchData() {
  return "Data received!";
}

fetchData().then((result) => console.log(result)); // Output: "Data received!"

What is await?

The await keyword is used inside an async function to pause its execution until a Promise is resolved or rejected. Once the Promise is settled, await returns the resolved value or throws an error if the Promise is rejected.

Key Points:

  1. Can only be used inside an async function.
  2. Pauses the function execution until the Promise is resolved.
  3. Improves readability by avoiding .then() chaining.

Example:

async function fetchData() {
  const data = await fetch("https://api.example.com/data");
  const json = await data.json();
  return json;
}

fetchData().then((result) => console.log(result));

Why Use async and await?

1. Improved Readability:

The async/await syntax makes asynchronous code look synchronous, improving its readability and maintainability.

With Promises:

fetch("https://api.example.com/data")
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error(error));

With async/await:

async function fetchData() {
  try {
    const response = await fetch("https://api.example.com/data");
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}

fetchData();

2. Avoid Callback Hell:

Callback hell occurs when multiple nested callbacks make code difficult to read and debug. By using async/await, you can write flat, clean code instead of deeply nested callbacks.

Without async/await:

getUser((user) => {
  getPosts(user.id, (posts) => {
    getComments(posts[0].id, (comments) => {
      console.log(comments);
    });
  });
});

With async/await:

async function getUserComments() {
  const user = await getUser();
  const posts = await getPosts(user.id);
  const comments = await getComments(posts[0].id);
  console.log(comments);
}

getUserComments();

3. Easier Error Handling:

Error handling with async/await is straightforward using try-catch blocks.

With Promises:

fetch("https://api.example.com/data")
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error("Error:", error));

With async/await:

async function fetchData() {
  try {
    const response = await fetch("https://api.example.com/data");
    const data = await response.json();
    console.log(data);
  } catch (error) {
    console.error("Error:", error);
  }
}

fetchData();

Important Notes and Limitations

  1. await can only be used in async functions: Trying to use await outside an async function will result in a syntax error.

    // Syntax Error
    const result = await somePromise();
  2. Parallel Execution: If you need to execute multiple asynchronous tasks in parallel, using await for each can make them run sequentially. Use Promise.all() for parallel execution.

    // Sequential Execution (Slower)
    const data1 = await fetchData1();
    const data2 = await fetchData2();
    
    // Parallel Execution (Faster)
    const [data1, data2] = await Promise.all([fetchData1(), fetchData2()]);
  3. Blocking Behavior: await pauses the execution of the async function but does not block the entire program.


Comparison of Promises and async/await

Feature Promises async/await
Syntax Chain-based (.then(), .catch()) Cleaner, flat, and synchronous-like
Error Handling .catch() for errors try-catch blocks
Parallel Execution Explicit (Promise.all()) Requires Promise.all() for parallel tasks
Readability Can become verbose for complex scenarios More readable and easier to maintain

Conclusion

The async and await keywords simplify working with asynchronous code by making it more readable and reducing complexity. They are ideal for tasks that involve multiple asynchronous operations, such as fetching data from APIs or processing files. By combining the power of Promises with a cleaner syntax, async and await enhance the developer experience and improve code quality.