Exp 3 Callback Promise and Async Await
Exp 3 Callback Promise and Async Await
A.3 Outcome:
After successful completion of this experiment students will be able to understand to concept of
Form Asycn Communication solve the issue of Callback hell in an Asynchronous Operation.
A.4 Theory
A callback is a function passed into another function as an argument to be executed later.
Callback Hell Callback Hell refers to a situation where callbacks are nested within other callbacks,
resulting in code that is difficult to read and maintain.
Solutions:
setTimeout(function(){
console.log("Hello World");
}, 2000);
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
Web Programming
Lab Manual
});
promise
.then(result => {
console.log(result);
return 'Next step';
})
.then(nextStep => {
console.log(nextStep); // Output: Next step
})
.catch(error => {
console.error(error);
});
Aync/Await is a feature in JavaScript that simplifies working with asynchronous code, making it more
readable and easier to understand.
// Using async/await
fetchDataAsync();
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
Web Programming
Lab Manual
A.5 Solving Callback Issue:
Given a food ordering process which follows of sequential execution with callbacks
function step1(callback) {
setTimeout(function () {
console.log("Step 1: Order placed successfully");
callback();
}, 2000);
}
function step2(callback) {
setTimeout(function () {
console.log("Step 2: Payment processed");
callback();
}, 4000);
}
function step3(callback) {
setTimeout(function () {
console.log("Step 3: Order being prepared");
callback();
}, 3000);
}
function finalStep() {
console.log("Step 4: Order delivered! Enjoy your meal!");
}
Problem1: convert the above food ordering process using Promises to handle the asynchronous
flow:
Promises: Each step returns a Promise that resolves when the task is done.
.then() chaining: Ensures that each step is executed in sequence once the previous
step is complete.
Error handling: a .catch() block is added to handle any potential errors in the promise
chain.
Problem2: By using the solution of Promlem1 where promise was used now convert the food
ordering scenario using async/await for better readability and easier handling of asynchronous
tasks:
async function: The processOrder function is declared as async so that we can use
await inside it.
await keyword: Ensures that each step is executed only after the previous step
completes.
Error handling: A try/catch block is used to catch any errors during the asynchronous
operations.
Cleaner flow: The use of async/await makes the code more synchronous-like, making
it easier to follow and maintain.
3. Removing unnecessary intermediate variables and making the code more streamlined.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
Web Programming
Lab Manual
const delay = (message, time) => new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, time));
function step1() {
return delay("Step 1: Order placed successfully", 2000);
}
function step2() {
return delay("Step 2: Payment processed", 4000);
}
function step3() {
return delay("Step 3: Order being prepared", 3000);
}
function finalStep() {
console.log("Step 4: Order delivered! Enjoy your meal!");
}
processOrder();
console.log("Other services are running in the background... bye!");
Hint:
const delay = (message, time) => new Promise(resolve => setTimeout(() =>
{ console.log(message); resolve(); }, time));
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
Web Programming
Lab Manual
PART B
(Students must submit the soft copy as per following segments within two hours of the practical. The
soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties
at the end of the practical in case the there is no Black board access available)
B.1 Code:
B.2 Output
(Take screen shots of the output at run time and paste it here)
B.3 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above)
(Students must write their observations and learnings as per the attainment of individual outcome listed
above)