Understanding Asynchronous JavaScript: Callbacks, Promises & Async/Await

Asynchronous JavaScript can feel overwhelming at first, especially for beginners. But with the right examples and a clear understanding, it becomes much easier to grasp. This article breaks down the core concepts—Callbacks, Promises, and Async/Await—in the simplest way possible, using relatable examples.

 

What Is Asynchronous JavaScript?

JavaScript runs code line by line, one at a time. But not all tasks finish immediately—some take time, like fetching data from a server or waiting for user input. Instead of freezing the entire program, JavaScript allows these long tasks to run in the background and continues with the rest of the code.

That’s what asynchronous behavior means—letting certain tasks take their time without blocking everything else.

 

1. Callbacks:-

A callback is a function passed into another function as an argument to be executed later—after a task finishes.

Example:- Think of asking a friend to submit an assignment. Once they do it, they give a call to confirm it’s done. That phone call? That’s the callback function.

Look at the below code:

 

The output of this is given below:-

 

2. Promises:

A Promise is an object that represents the eventual result of an asynchronous operation. It has three states:

  • Pending
  • Resolved (task completed successfully)
  • Rejected (an error occurred)

 Example:- Ordering food at the college canteen. The staff says, “Come back in 10 minutes, your food will be ready.” That’s a promise. If the food is ready on time—resolved. If something goes wrong—rejected.

The example code is given below:

 

The output of above code is shown below:

 

3. Async/Await:-

Async/Await is a modern way to handle promises that looks like regular, synchronous code. It’s built on top of promises but makes code much easier to read and write.

Example:  Instead of waiting around near the canteen, imagine just chilling until someone tells you, “Your food is ready.” You wait calmly, then eat when it’s done. That’s await.

The example code is given below:

 

The output of above code is given below:

 

Understanding how to work with asynchronous code is a key skill in JavaScript development.

  • Callbacks are useful but can become messy. It is useful for small tasks.
  • Promises make async code cleaner. Easy to handle errors.
  • Async/Await is the most readable and modern approach. It often looks like synchronous code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top