Home Coding & Scripting Understanding Async and Promises in JavaScript

Understanding Async and Promises in JavaScript

239
0

JavaScript is an amazing language, but many beginners get confused when they first hear terms like asynchronous code, Promises, async, await, and callbacks.

If these concepts seem overwhelming—don’t worry.
This guide explains everything in the simplest way possible, with clear examples and clean code.


1. Why Does JavaScript Need Async?

JavaScript runs on a single thread—it can execute only one task at a time.

But modern applications need to:

  • fetch data from APIs
  • handle user interactions
  • wait for timers
  • read files

If JavaScript paused everything for each slow operation, your entire website or app would freeze.

✅ The solution: Asynchronous programming

It allows JavaScript to continue running while waiting for slow tasks to complete.


2. What Is a Promise? (Beginner-Friendly Explanation)

A Promise in JavaScript is like a guarantee:

“I don’t have the value right now… but I’ll give it to you later.”

A Promise has three states:

StateMeaning
PendingThe work is still happening
FulfilledThe task succeeded and returned a value
RejectedThe task failed with an error

Example: A Promise Showing All States

console.log("Promise is created...");

let promise = new Promise((resolve, reject) => {
    console.log("State: PENDING");

    setTimeout(() => {
        let success = true; // Change to false to simulate rejection

        if (success) {
            resolve("State: FULFILLED → Data fetched successfully!");
        } else {
            reject("State: REJECTED → Something went wrong!");
        }
    }, 1500);
});

promise
    .then(result => console.log(result))
    .catch(error => console.log(error))
    .finally(() => console.log("Promise is settled (fulfilled or rejected)."));

What You’ll See in the Console

  • Immediately → PENDING
  • After 1.5 seconds:
    • ✔ Fulfilled (if success = true)
    • ❌ Rejected (if success = false)
  • Finally → “Promise is settled”

This is the simplest way to understand Promise states.


3. Visualizing the Promise Lifecycle

          Pending
         /       \
 Fulfilled     Rejected

4. What Are async and await?

async and await are built on top of Promises.
They make asynchronous code look and feel like normal code.

Example: Using async/await

function fetchData() {
    return new Promise(resolve => {
        setTimeout(() => resolve("Data fetched!"), 1000);
    });
}

async function showData() {
    let result = await fetchData();  
    console.log(result);
}

showData();

Important Note

  • await pauses inside the async function
  • It does not block the entire program

This is why async/await code looks cleaner and more readable.


5. Promises vs Async/Await: What’s the Difference?

Promises (.then)Async/Await
Uses .then()Uses await
More chainingCleaner and more readable
Good for parallel tasksGood for step-by-step tasks
Can become longLooks like simple synchronous code

6. Real Example: Fetching Data from an API

Using Promises

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

Using async/await

async function getData() {
    try {
        let res = await fetch("https://api.example.com/data");
        let data = await res.json();
        console.log(data);
    } catch (err) {
        console.error(err);
    }
}
getData();

Much cleaner, right?


7. How Promise Chaining Works

new Promise(resolve => resolve(5))
    .then(num => num * 2)   // 10
    .then(num => num + 10)  // 20
    .then(result => console.log(result)); // Output: 20

Each .then() receives the previous result.


8. Useful Promise Helper Methods

JavaScript provides powerful helpers for asynchronous tasks:


✔ Promise.all()

Runs multiple tasks in parallel and waits for all to finish.

Promise.all([task1, task2, task3])
    .then(results => console.log(results));

✔ Promise.race()

Returns the result of the first Promise that completes.


✔ Promise.allSettled()

Waits for all Promises, regardless of whether they fulfill or reject.


9. When Should You Use What?

SituationBest Choice
Step-by-step tasksasync/await
Many tasks running at oncePromise.all
Need the fastest resultPromise.race
Beginners learning async codeasync/await

10. Common Beginner Mistakes (Read This!)

❌ Forgetting to mark a function as async before using await

❌ Mixing .then() and await unnecessarily in the same block

❌ Not using try/catch to handle errors with async/await


11. Summary

  • JavaScript uses asynchronous programming to stay fast and responsive.
  • A Promise represents a value that will be available in the future.
  • async functions always return a Promise.
  • await lets you write asynchronous code that looks like normal code.
  • async/await is great for readability, while Promise utilities handle complex workflows.
Previous articleStyling React Components Using CSS, CSS Modules, and More
Next articleHow Side Effects (useEffect) Work in React and Next.js
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here