let order = (call_production) => {
setTimeout(() => {
call_production();
}, 1000);
};
let production = () => {
setTimeout(() => {
console.log("step 1");
setTimeout(() => {
console.log("step 2");
setTimeout(() => {
console.log("step 3");
setTimeout(() => {
console.log("step 4");
setTimeout(() => {
console.log("step 5");
setTimeout(() => {
console.log("step 6");
setTimeout(() => {
console.log("step 7");
setTimeout(() => {
console.log("step 8");
setTimeout(() => {
console.log("step 9");
setTimeout(() => {
console.log("step 10");
setTimeout(() => {
console.log("A call backhell");
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
};
order(production);
The cause of callback hell is when people try to write JavaScript in a way where execution happens visually from top to bottom.
Lots of people make this mistake!
In other languages like C, Ruby or Python there is the expectation that whatever happens on line 1 will finish before the code on line 2 starts running and so on down the file.
As you will learn, JavaScript is different.
fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function (width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})
let production = () =>{
setTimeout(()=>{
console.log("production has started")
setTimeout(()=>{
console.log("The fruit has been chopped")
setTimeout(()=>{
console.log(`${stocks.liquid[0]} and ${stocks.liquid[1]} Added`)
setTimeout(()=>{
console.log("start the machine")
setTimeout(()=>{
console.log(`Ice cream placed on ${stocks.holder[1]}`)
setTimeout(()=>{
console.log(`${stocks.toppings[0]} as toppings`)
setTimeout(()=>{
console.log("serve Ice cream")
},2000)
},3000)
},2000)
},1000)
},1000)
},2000)
},0000)
};