TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses ยท 55.7K subscribers
Post #3542 1.81K
JavaScript Asynchronous Programming ๐Ÿ‘จโ€๐Ÿ’ป

Asynchronous programming allows JavaScript to perform long-running tasks without freezing the application.

Without asynchronous programming:

โ€ข Websites would become unresponsive while waiting for data

โ€ข Users would have to wait for one task to finish before doing anything else

This concept is essential for API calls, timers, file handling, and modern web applications.

1. What is Synchronous Programming?

In synchronous programming, code executes one line at a time.

The next line waits until the current line finishes.

Example:

console.log("Start");
console.log("Middle");
console.log("End");


Output:

Start

Middle

End

2. What is Asynchronous Programming?

In asynchronous programming, long-running tasks run in the background, allowing other code to continue executing.

Example:

console.log("Start");
setTimeout(() => {
console.log("Task Completed");
}, 2000);
console.log("End");


Output:

Start

End

Task Completed

3. What is the Call Stack?

The Call Stack is a data structure that keeps track of function execution.

Functions are added to the stack when called and removed after execution.

Example:

function first() {
second();
}
function second() {
console.log("Hello");
}
first();


Execution Order:

1. first() is pushed onto the stack

2. second() is pushed

3. console.log() executes

4. second() is removed

5. first() is removed

4. What is the Callback Queue?

The Callback Queue stores asynchronous callbacks until the Call Stack is empty.

Example:

setTimeout(() => {
console.log("Executed");
}, 1000);


The callback waits in the queue until JavaScript is ready to execute it.

5. What is the Event Loop?

The Event Loop continuously checks:

โ€ข Is the Call Stack empty?

โ€ข If yes, move callbacks from the Callback Queue to the Call Stack

Example:

console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
console.log("End");


Output:

Start

End

Timeout

Even with a delay of 0, the callback executes only after the Call Stack is empty.

6. Callback Functions

A callback is a function passed as an argument to another function.

Example:

function greet(name, callback) {
console.log("Hello " + name);
callback();
}
function completed() {
console.log("Done");
}
greet("Deepak", completed);


Output:

Hello Deepak

Done

7. Callback Hell

Nested callbacks make code difficult to read and maintain.

Example:

getUser(function(user) {
getOrders(user, function(orders) {
getPayment(orders, function(payment) {
console.log(payment);
});
});
});


Problems:

โ€ข Hard to read

โ€ข Difficult to debug

โ€ข Difficult to maintain

8. What is a Promise?

A Promise represents the eventual completion or failure of an asynchronous operation.

Promise States:

โ€ข Pending

โ€ข Fulfilled

โ€ข Rejected

Example:

const promise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Success");
} else {
reject("Failed");
}
});

promise.then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});


9. Promise Chaining

Multiple .then() methods can be chained together.

Example:
  • โค 5
More from @javascript_courses
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Oct 4, 2026DSA Handwritten Notes
  5. Sep 28, 2026To learn web design and development from basic to advanced levels, you can follow these stโ€ฆ
  6. Sep 27, 202610 Websites Every Developer & AI Enthusiast Should Bookmark โœ… roadmap.sh โ€“ Step-by-step leโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’