DEV Community

Dharan Ganesan
Dharan Ganesan

Posted on

Day 31: Async Await

The introduction of async and await in ECMAScript 2017 (ES8) revolutionized how developers deal with asynchronous tasks, making code more readable and maintainable. Before async and await, developers relied on callbacks and promises to manage asynchronous operations. Callbacks led to the infamous "callback hell," while promises improved code readability. However, promises still required handling .then() and .catch() chains, which could get complex in more intricate scenarios.

๐Ÿ„ Enter async/await

async and await are built on top of promises and offer a more elegant and intuitive way to work with asynchronous code. async is used to declare an asynchronous function, and within that function, you can use await to pause execution until a promise is resolved.

async function fetchData(url) {
  try {
    const response = await fetch(url);
    const data = await response.json();
    return data;
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error;
  }
}

// Usage
fetchData("https://api.example.com/data")
  .then(result => console.log(result))
  .catch(error => console.error(error));
Enter fullscreen mode Exit fullscreen mode

Benefits of async/await

  1. Readability: Asynchronous code reads almost like synchronous code, making it easier for developers to understand the flow of execution.

  2. Error Handling: With try-catch blocks, error handling becomes more natural and localized within the function, improving debugging.

  3. Sequential Code: await allows you to write sequential code even when dealing with asynchronous tasks, enhancing the logical structure of your program.

  4. Promise Chaining Reduction: async/await eliminates long chains of .then() and .catch() calls, leading to cleaner code.

  5. Concurrent Asynchronous Calls: You can use Promise.all() with await to perform multiple asynchronous operations concurrently.

async function fetchMultipleData(urls) {
  const promises = urls.map(url => fetchData(url));
  const results = await Promise.all(promises);
  return results;
}

// Usage
fetchMultipleData([
  "https://api.example.com/data/1",
  "https://api.example.com/data/2"
]).then(results => console.log(results));
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Potential Pitfalls and Best Practices

  1. Not Always Necessary: Not every function needs to be async. Only use it when you need to pause the function's execution to wait for a promise to resolve.

  2. Avoid Blocking: Although await can block the function, it doesn't block the whole thread, ensuring other tasks can still be executed.

  3. Unhandled Promise Rejections: Ensure you have appropriate error handling in place using try-catch or .catch() to prevent unhandled promise rejections.

  4. Sequential vs. Parallel: Be mindful of whether tasks should be executed sequentially or in parallel, and structure your code accordingly.

Top comments (1)

Collapse
 
robinamirbahar profile image
Robina

Amazing