๐Ÿš€ Day 12 of #30DaysOfJavaScript: Exploring Async and Await in JavaScript

Today, we dove into the world of async and awaitโ€”a key part of handling asynchronous code in JavaScript. If you're wondering what asynchronous code is and why it's so crucial, let's break it down.

๐ŸŒ What is Asynchronous Code?

Asynchronous code allows JavaScript to start tasks that take time, like fetching data from a server, without waiting for them to finish. Instead, it โ€œsets them asideโ€ and continues with other tasks. This is crucial for keeping apps responsive, especially when dealing with network requests or large calculations.

Without an async code, your program would freeze up every time it had to wait, frustrating users.

๐Ÿš€ Enter Async and Await

Async and await are newer keywords in JavaScript, introduced to simplify working with asynchronous operations. They make async code look more like โ€œnormalโ€ synchronous code, which is easier to read and understand.

๐Ÿ’ก Getting Started with Async and Await

  • Async Functions: By adding async before a function, JavaScript knows that it will contain asynchronous code.

      async function getData() {
          // async code here
      }
    
  • Await: Inside an async function, await pauses the function until the awaited task is completed. This allows us to directly work with the final result without chaining .then() calls.

      async function getData() {
          let response = await fetch('https://api.example.com/data');
          let data = await response.json();
          console.log(data);
      }
    

๐Ÿ” Why Use Async and Await?

Async/await simplifies how we handle async tasks, making it easier to read and maintain code. It also reduces โ€œcallback hellโ€ and makes error handling more straightforward with try...catch blocks.

๐ŸŒŸ Practical Example with Async and Await

Letโ€™s see how to fetch data using async/await:

async function fetchUser() {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/users/1');
        const user = await response.json();
        console.log('User:', user);
    } catch (error) {
        console.error("Error fetching data:", error);
    }
}

fetchUser();

In this example:

  • await pauses execution until fetch() completes.

  • If an error occurs, itโ€™s caught in the catch block, keeping the code cleaner and easier to debug.

๐Ÿš€ Wrapping Up

Today, async/await unlocked a new level of readability for our async JavaScript code. Try experimenting with async/await in your projects, and see how it can simplify async programming!

Stay tuned for tomorrow, where weโ€™ll dive into the world of JSON and explore how to handle, parse, and work with data in JavaScript!


0
Subscribe to my newsletter

Read articles from Stanley Owarieta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Stanley Owarieta
Stanley Owarieta

๐—”๐˜€๐—ฝ๐—ถ๐—ฟ๐—ถ๐—ป๐—ด ๐—™๐—ฟ๐—ผ๐—ป๐˜๐—ฒ๐—ป๐—ฑ ๐——๐—ฒ๐˜ƒ๐—ฒ๐—น๐—ผ๐—ฝ๐—ฒ๐—ฟ passionate about ๐™…๐™–๐™ซ๐™–๐™Ž๐™˜๐™ง๐™ž๐™ฅ๐™ฉ, ๐™˜๐™ค๐™™๐™ž๐™ฃ๐™œ, ๐™–๐™ฃ๐™™ ๐™—๐™ช๐™ž๐™ก๐™™๐™ž๐™ฃ๐™œ ๐™ข๐™ฎ ๐™›๐™ช๐™ฉ๐™ช๐™ง๐™š ๐™ž๐™ฃ ๐™ฉ๐™š๐™˜๐™. Along with my ๐™ก๐™ค๐™ซ๐™š for ๐™›๐™–๐™จ๐™๐™ž๐™ค๐™ฃ, ๐™œ๐™–๐™ข๐™ž๐™ฃ๐™œ, ๐™–๐™ฃ๐™™ ๐™ก๐™ช๐™ญ๐™ช๐™ง๐™ฎ ๐™ก๐™ž๐™ซ๐™ž๐™ฃ๐™œ, I have big dreams like ๐™ค๐™ฌ๐™ฃ๐™ž๐™ฃ๐™œ ๐™– ๐™ฅ๐™ง๐™ž๐™ซ๐™–๐™ฉ๐™š ๐™Ÿ๐™š๐™ฉ and ๐™ก๐™ž๐™ซ๐™ž๐™ฃ๐™œ ๐™ž๐™ฃ ๐™– ๐™ก๐™ช๐™ญ๐™ช๐™ง๐™ฎ ๐™๐™ค๐™ข๐™š ๐™ค๐™ฃ๐™š ๐™™๐™–๐™ฎ. Since 2021, Iโ€™ve invested in ๐—”๐—ฝ๐—ฝ๐—น๐—ฒ, ๐—”๐—บ๐—ฎ๐˜‡๐—ผ๐—ป, ๐—ฆ๐—ต๐—ผ๐—ฝ๐—ถ๐—ณ๐˜†, ๐—ฎ๐—ป๐—ฑ ๐—š๐—ผ๐—ผ๐—ด๐—น๐—ฒโ€”working toward financial independence. I also look forward to being a ๐—น๐—ผ๐˜ƒ๐—ถ๐—ป๐—ด ๐—ณ๐—ฎ๐˜๐—ต๐—ฒ๐—ฟ ๐—ฎ๐—ป๐—ฑ ๐—ฎ ๐—ฑ๐—ฒ๐˜ƒ๐—ผ๐˜๐—ฒ๐—ฑ ๐—ฝ๐—ฎ๐—ฟ๐˜๐—ป๐—ฒ๐—ฟ, growing a ๐—บ๐—ถ๐—น๐—น๐—ถ๐—ผ๐—ป-๐—ฑ๐—ผ๐—น๐—น๐—ฎ๐—ฟ ๐—ฏ๐—ฟ๐—ฎ๐—ป๐—ฑ together with my ๐—ณ๐˜‚๐˜๐˜‚๐—ฟ๐—ฒ ๐˜„๐—ถ๐—ณ๐—ฒ. Letโ€™s connect and inspire each other on this journey!