Mastering Async & Await: Making JavaScript Fun and Simple

Understanding JavaScript();

Madhav Mansuriya

--

Hey there, future coding wizards! 🌟 Today, we’re diving into a cool part of JavaScript that makes your code clean and powerful: Async & Await. Don’t worry if you’re new to this — I’ll make it easy and fun!

To get the most out of this blog, you should have a basic understanding of JavaScript and synchronous vs. asynchronous code execution.

Let’s start with a small example

Have you been to the restaurant before? I know you have (silly me asking silly questions 😅 ). Let’s assume you’ve ordered something, but all the plates are being washed. What will the waiter do? Will he directly put the food on your table, or will he wait for the plates to be washed and then serve the food?

Here’s how it will look if you put it into the JS code.

The food’s all set to be served, but we need to wash the plates first. In our code, we’re waiting for those plates to be sparkling clean before serving the meal.

JavaScript normally runs code line by line, without pausing for lengthy tasks like washing plates. If we didn’t use async and await, the washPlates function would be called, but JavaScript wouldn't wait for it to finish before moving on.

By marking our function as async and using await before washPlates function, we tell JavaScript to chill out and wait until the plates are clean before it serves the food. This way, our code runs smoothly, just like a well-organized kitchen! 🍽️

Exactly! Now you’ve got a tasty understanding of async and await, just like enjoying a perfectly served meal at a restaurant.

now feeling like a hero? 😎

hero.

Let’s dive a bit deeper.

What’s the Big Deal with Async & Await?

In JavaScript, dealing with tasks that take time, like fetching data from a website or reading files, can be tricky. If you’re not careful, your code can become a tangled mess of “callbacks” and “promises.”

Async & Await is like having a magic wand to make this easier. They help you write cleaner and more readable code, avoiding the “callback hell” that makes your head spin.

Let’s Break It Down

1. Async Function

Think of an async function as a special kind of function that always returns a promise. Inside it, you can use await to pause the function until a promise is resolved.

2. Await Keyword

The await keyword can only be used inside async functions. It makes your code wait until a promise is resolved before moving on.

using await keyword

Real-World Examples

Example 1: Fetching Data from an API

Imagine you’re making an app that shows user profiles. You need to fetch data from an API. Here’s how Async & Await makes this super easy:

Example 1

Example 2: Waiting for a Timer

Sometimes, you need to wait for a certain amount of time. Here’s how you can do that with Async & Await:

Example 2

Wrapping Up

Async & Await makes handling asynchronous tasks in JavaScript smooth and simple. It helps you avoid messy code and focus on what your program should do, not how to manage the timing.

So, go ahead and sprinkle some async magic in your code. Happy coding! 🚀

The food’s all set to be served, but we need to wash the plates first. In our code, we’re waiting for those plates to be sparkling clean before serving the meal.

JavaScript normally runs code line by line, without pausing for lengthy tasks like washing plates. If we didn’t use async and await, the washPlates function would be called, but JavaScript wouldn't wait for it to finish before moving on.

By marking our function as async and using await before washPlates, we tell JavaScript to chill out and wait until the plates are clean before it serves the food. This way, our code runs smoothly, just like a well-organized kitchen! 🍽️

--

--

Madhav Mansuriya
Madhav Mansuriya

No responses yet