DEV Community

Cover image for Exploring Asynchronous JavaScript: Callbacks, Promises, and Async/Await
Jack Pritom Soren
Jack Pritom Soren

Posted on

Exploring Asynchronous JavaScript: Callbacks, Promises, and Async/Await

JavaScript is a single-threaded language, which means it can only perform one operation at a time. However, web applications often need to perform multiple operations simultaneously, like fetching data from a server, reading files, or handling user inputs. To manage these tasks efficiently, JavaScript provides mechanisms for asynchronous programming. In this blog post, we will explore three key concepts in asynchronous JavaScript: callbacks, promises, and async/await.

Callbacks: The Original Asynchronous Pattern

A callback is a function passed into another function as an argument and is executed after some operation has been completed. This is one of the simplest ways to handle asynchronous operations in JavaScript.

Example of Callbacks

function fetchData(callback) {
    setTimeout(() => {
        console.log("Data fetched from server");
        callback();
    }, 2000);
}

function processData() {
    console.log("Processing data...");
}

fetchData(processData);
Enter fullscreen mode Exit fullscreen mode

In this example, fetchData simulates fetching data from a server with a delay using setTimeout. Once the data is fetched, the processData function is called.

Drawbacks of Callbacks

While callbacks are straightforward, they can lead to callback hell, a situation where callbacks are nested within callbacks, making the code difficult to read and maintain.

function fetchData(callback) {
    setTimeout(() => {
        console.log("Data fetched from server");
        callback();
    }, 2000);
}

function processData(callback) {
    console.log("Processing data...");
    callback();
}

function displayData() {
    console.log("Displaying data...");
}

fetchData(() => {
    processData(() => {
        displayData();
    });
});
Enter fullscreen mode Exit fullscreen mode

Promises: A Cleaner Approach

Promises offer a more elegant way to handle asynchronous operations. A promise represents a value that may be available now, in the future, or never. It can be in one of three states: pending, fulfilled, or rejected.

Example of Promises

function fetchData() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("Data fetched from server");
        }, 2000);
    });
}

fetchData()
    .then((message) => {
        console.log(message);
        return "Processing data...";
    })
    .then((message) => {
        console.log(message);
        return "Displaying data...";
    })
    .then((message) => {
        console.log(message);
    })
    .catch((error) => {
        console.error("Error:", error);
    });
Enter fullscreen mode Exit fullscreen mode

In this example, fetchData returns a promise. The then method is used to handle the resolved value of the promise, allowing us to chain asynchronous operations in a readable manner.

Advantages of Promises

  • Chaining: Promises can be chained, improving code readability.
  • Error Handling: Promises provide a catch method to handle errors, which helps in managing error states more effectively.

Async/Await: The Syntactic Sugar

Async/await is a newer syntax introduced in ES2017 (ES8) that allows us to write asynchronous code that looks synchronous. It is built on top of promises and makes the code easier to read and write.

Example of Async/Await

function fetchData() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("Data fetched from server");
        }, 2000);
    });
}

async function handleData() {
    try {
        const data = await fetchData();
        console.log(data);
        console.log("Processing data...");
        console.log("Displaying data...");
    } catch (error) {
        console.error("Error:", error);
    }
}

handleData();
Enter fullscreen mode Exit fullscreen mode

In this example, the fetchData function is the same as before, returning a promise. The handleData function uses the async keyword, and within this function, we use await to pause the execution until the promise is resolved.

Benefits of Async/Await

  • Readability: Makes asynchronous code look like synchronous code, enhancing readability.
  • Error Handling: Uses try/catch blocks for error handling, which is familiar to those with experience in synchronous programming.

Conclusion

Asynchronous programming is a vital part of JavaScript, enabling applications to perform multiple tasks efficiently without blocking the main thread. Callbacks were the original way to handle async operations but can lead to complex and hard-to-read code. Promises offer a cleaner and more manageable approach with chaining and better error handling. Async/await builds on promises, providing a more intuitive and readable syntax for writing asynchronous code. Understanding these concepts and their use cases will help you write more efficient and maintainable JavaScript code.

Follow me on : Github Linkedin

Top comments (0)