Category: javascript
Async Programming in JavaScript
Published on 13 Jun 2026
Explanation
Async Programming in JavaScript allows
long-running
operations to execute without blocking the
main thread. JavaScript uses an Event
Loop to manage asynchronous tasks.
Code:
console.log('Start');
setTimeout(() => {
console.log('Task Completed');
}, 2000);
console.log('End');
Explanation
Callbacks are functions executed after an
asynchronous operation completes.
Code:
function fetchData(callback) {
setTimeout(() => {
callback('Data Received');
}, 2000);
}
fetchData(data => console.log(data));
Explanation
Promises provide a better way to
handle asynchronous operations and
avoid callback
nesting.
Code:
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve('Success');
}, 2000);
});
promise.then(result => console.log(result));
Explanation
Async/Await offers a cleaner and more
readable syntax for working with Promises.
Code:
async function getData() {
const result = await Promise.
resolve('Data Received');
console.log(result);
}
getData();
Explanation
Execution Flow and Interview Answer: Async
tasks run outside the main thread
and return results when completed. This
keeps applications responsive.
Code:
Main Thread --> Web APIs -->
Callback Queue --> Event Loop -->
Main Thread
async function example() {
const result =
await someAsyncOperation();
return result;
}