DEV Community

Cover image for Embracing Asynchronous JavaScript: A Deep Dive🚀
Dharmendra Kumar
Dharmendra Kumar

Posted on

Embracing Asynchronous JavaScript: A Deep Dive🚀

Asynchronous JavaScript is essential for creating dynamic, non-blocking web applications. This guide explores the core concepts, tools, and techniques for effectively handling asynchronous operations in JavaScript.

Introducing Asynchronous JavaScript

Asynchronous JavaScript allows tasks to run in the background, enabling your application to remain responsive and efficient. It's a key feature for modern web development, allowing for tasks like data fetching, file reading, and timers to be executed without blocking the main thread.

Example:

console.log("Start");

setTimeout(() => {
    console.log("This runs after 2 seconds");
}, 2000);

console.log("End");
// Output: Start, End, This runs after 2 seconds
Enter fullscreen mode Exit fullscreen mode

Why and Where to Use Asynchronous JavaScript

Asynchronous JavaScript is used to enhance user experience by:

  • Preventing Freezing: Avoiding UI freezing during time-consuming operations.
  • Improving Performance: Allowing multiple operations to run concurrently.
  • Handling Network Requests: Making API calls without blocking the main thread.

Example:

fetch("https://api.example.com/data")
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error("Error:", error));
Enter fullscreen mode Exit fullscreen mode

How to Use Promises

Promises are objects representing the eventual completion or failure of an asynchronous operation. They provide a cleaner, more intuitive way to handle asynchronous tasks compared to callbacks.

Example:

let promise = new Promise((resolve, reject) => {
    let success = true;
    if (success) {
        resolve("Operation was successful");
    } else {
        reject("Operation failed");
    }
});

promise.then(result => {
    console.log(result);
}).catch(error => {
    console.error(error);
});
Enter fullscreen mode Exit fullscreen mode

How to Implement a Promise-Based API

Creating a promise-based API involves wrapping asynchronous operations within a promise.

Example:

function fetchData(url) {
    return new Promise((resolve, reject) => {
        fetch(url)
            .then(response => response.json())
            .then(data => resolve(data))
            .catch(error => reject(error));
    });
}

fetchData("https://api.example.com/data")
    .then(data => console.log(data))
    .catch(error => console.error("Error:", error));
Enter fullscreen mode Exit fullscreen mode

Introducing Workers

Web Workers allow you to run scripts in background threads, enabling parallel execution and improving performance.

Example:

// worker.js
self.onmessage = function(e) {
    let result = e.data * 2;
    self.postMessage(result);
};

// main.js
let worker = new Worker('worker.js');
worker.onmessage = function(e) {
    console.log('Result from worker:', e.data);
};
worker.postMessage(10);
Enter fullscreen mode Exit fullscreen mode

Sequencing Animations

Asynchronous JavaScript can be used to create complex, sequenced animations.

Example:

function animateBox(element) {
    return new Promise((resolve) => {
        element.style.transition = "transform 2s";
        element.style.transform = "translateX(100px)";
        element.addEventListener("transitionend", resolve, { once: true });
    });
}

async function runAnimation() {
    let box = document.getElementById("box");
    await animateBox(box);
    console.log("Animation complete");
}

runAnimation();
Enter fullscreen mode Exit fullscreen mode

Adding Features to Our Bouncing Balls Demo

Enhancing the bouncing balls demo with asynchronous JavaScript can add more dynamic interactions and animations.

Example:

// Bouncing balls demo with asynchronous gravity effect
class Ball {
    constructor(x, y, dx, dy) {
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;
        this.radius = 10;
    }

    draw(context) {
        context.beginPath();
        context.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
        context.fillStyle = "blue";
        context.fill();
        context.closePath();
    }

    async applyGravity() {
        this.dy += 0.5; // Simulating gravity
        await new Promise(resolve => setTimeout(resolve, 100)); // Pausing for effect
    }

    async update(canvasWidth, canvasHeight) {
        if (this.x + this.dx > canvasWidth - this.radius || this.x + this.dx < this.radius) {
            this.dx = -this.dx;
        }
        if (this.y + this.dy > canvasHeight - this.radius || this.y + this.dy < this.radius) {
            this.dy = -this.dy;
        }
        await this.applyGravity();
        this.x += this.dx;
        this.y += this.dy;
    }
}

let canvas = document.getElementById("myCanvas");
let context = canvas.getContext("2d");
let ball = new Ball(50, 50, 2, 2);

async function animate() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    ball.draw(context);
    await ball.update(canvas.width, canvas.height);
    requestAnimationFrame(animate);
}

animate();
Enter fullscreen mode Exit fullscreen mode

Additional Topics

Async/Await

Async/await syntax simplifies working with promises, making asynchronous code look synchronous.

Example:

async function fetchData() {
    try {
        let response = await fetch("https://api.example.com/data");
        let data = await response.json();
        console.log(data);
    } catch (error) {
        console.error("Error:", error);
    }
}

fetchData();
Enter fullscreen mode Exit fullscreen mode

Error Handling with Async Functions

Handling errors in asynchronous functions is straightforward with try/catch blocks.

Example:

async function fetchData() {
    try {
        let response = await fetch("https://api.example.com/data");
        if (!response.ok) {
            throw new Error("Network response was not ok");
        }
        let data = await response.json();
        console.log(data);
    } catch (error) {
        console.error("Fetch error:", error);
    }
}

fetchData();
Enter fullscreen mode Exit fullscreen mode

Managing Multiple Promises

Using Promise.all and Promise.race to handle multiple asynchronous operations simultaneously.

Example:

let promise1 = fetch("https://api.example.com/data1").then(response => response.json());
let promise2 = fetch("https://api.example.com/data2").then(response => response.json());

Promise.all([promise1, promise2])
    .then(results => {
        console.log("Data from both endpoints:", results);
    })
    .catch(error => {
        console.error("Error:", error);
    });
Enter fullscreen mode Exit fullscreen mode

By mastering these asynchronous JavaScript techniques, you can build highly responsive, efficient, and user-friendly web applications. Happy coding!

Top comments (9)

Collapse
 
njongo_magagula_2e65cb8df profile image
Njongo Magagula

Hi sir can you please help with that request

Collapse
 
dharamgfx profile image
Dharmendra Kumar

wait bro

Collapse
 
njongo_magagula_2e65cb8df profile image
Njongo Magagula

Hey sir you see if you know how to help me please do i dont like to be left hanging because now it will look like i am annoying you when we talked progress but you left me into thin air hey bro we are all hustling out here life is toughf so from a brother to brother please help who know maybe you will make me a better person but all in all never refuse to help peaple in this world man so bro if you think i am a neard or what cool bro because i tried my best to get your help but i guess it is what it is

Thread Thread
 
dharamgfx profile image
Dharmendra Kumar

Hey bro,
I have received your email, but I am unable to access your git repository. Could you please check?

Collapse
 
njongo_magagula_2e65cb8df profile image
Njongo Magagula

Thank you good lesson

Collapse
 
dharamgfx profile image
Dharmendra Kumar

Welcome bro

Collapse
 
njongo_magagula_2e65cb8df profile image
Njongo Magagula

Hey mr can you help me with my project i wrote you an email i am trying to reach out to you for help i really need your help with my bookmarkelt can we try to talk if you have time please i can really appriciate your help

Collapse
 
darshank profile image
Darshan Keskar

can you Explain More about Asynchronous calls ...

Collapse
 
dharamgfx profile image
Dharmendra Kumar

Yes, I will definitely wait for new blogs.