/** * 1. Replace regular functions with arrow functions * 2. Fix callback hell by rewriting it with async/await * 3. Mak

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

/** * 1. Replace regular functions with arrow functions * 2. Fix callback hell by rewriting it with async/await * 3. Mak

Post by answerhappygod »

/**
* 1. Replace regular functions with arrow functions
* 2. Fix callback hell by rewriting it with async/await
* 3. Make sure the "Finish" is logged after all the data isconverted
*/
function timeout(ms, callback) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();
callback();
}, ms);
});
}
function generateRandomNumber() {
return Math.floor(Math.random() * 40);
}
function generateData(callback) {
timeout(1000, function () {
const data = Array.from({ length: 20 },generateRandomNumber);
callback(data);
});
}
function convertToFeet(meters, callback) {
const feet = meters * 3.2808;
timeout(3500, function () {
callback(feet);
});
}
function processData(data, callback) {
data.map(function (value) {
callback(value);
});
}
function logResult(meters, feet) {
console.log(`Converted ${meters}m to ${feet}ft`);
}
function main() {
console.log("Start");
generateData(function (data) {
processData(data, function (value) {
convertToFeet(value, function (result) {
logResult(value, result);
});
});
});
console.log("Finish");
}
main();
# Assignment
- Refactor cb-hell.js:​
- Replace regular functions with arrow functions​
- Fix callback hell by rewriting it with async/await​
- Make sure the "Finish" is logged after all the data isconverted ​
- Attach screenshot of the output in your terminal
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply