/**
* 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
/** * 1. Replace regular functions with arrow functions * 2. Fix callback hell by rewriting it with async/await * 3. Mak
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am