what is Asynchronous JavaScript

2 min readJan 19, 2024

Asynchronous JavaScript is a programming paradigm that allows certain operations to be performed concurrently without blocking the execution of the program.

Asynchronous programming is commonly used in web development to handle tasks such as fetching data from servers, handling user input, and managing timers.

There are several mechanisms in JavaScript for handling asynchronous operations, including callbacks, promises, and the more recent async/await syntax.

01.callback

callback function is function which is passed as argument in anather function and exicuted later after complition of asynchronus function

function fetchData(callback) {
setTimeout(() => {
console.log("Data fetched!");
callback();
}, 1000);
}

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

fetchData(processCallback);

2.Promises

Definition: Promises are objects representing the eventual completion or failure of an asynchronous operation. They provide a cleaner and more structured way to handle asynchronous code compared to callbacks.

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

fetchData()
.then(() => {
console.log("Processing data...");
})
.catch((error) => {
console.error("Error:", error);
});

3.Async/Await:

Async/await is a syntax for handling asynchronous code that was introduced in ECMAScript 2017 (ES8). It provides a more concise and synchronous-like way to work with asynchronous operations using promises.

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

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

async function fetchDataAndProcess() {
await fetchData();
processData();
}

fetchDataAndProcess();

Async/await simplifies the syntax for working with promises and makes asynchronous code easier to read and write, especially when dealing with multiple asynchronous operations.

It is built on top of promises and provides a more natural and sequential approach to asynchronous programming

--

--

PROFESSOR !!
PROFESSOR !!

Written by PROFESSOR !!

start the journey with me and be a master in js and react to beacome a frontend developer

Responses (1)