Callback hell
Course: Promises and async, await - chapter 6
Q: Write code that
- logs
hiafter 1 second - logs
hello3 seconds afterstep 1 - logs
hello there5 seconds afterstep 2
Solution (has callback hell)
setTimeout(function () { console.log("hi"); setTimeout(function () { console.log("hello");
setTimeout(function () { console.log("hello there"); }, 5000); }, 3000);}, 1000);Alt solution (doesnt really have callback hell)
function step3Done() { console.log("hello there");}
function step2Done() { console.log("hello"); setTimeout(step3Done, 5000);}
function step1Done() { console.log("hi"); setTimeout(step2Done, 3000);}
setTimeout(step1Done, 1000);Promisified version
Section titled “Promisified version”Now use the promisified version we saw in the last slide
function setTimeoutPromisified(ms) { return new Promise(resolve => setTimeout(resolve, ms));}Solution #1 (has callback hell)
function setTimeoutPromisified(ms) { return new Promise((resolve) => setTimeout(resolve, ms));}
setTimeoutPromisified(1000).then(function () { console.log("hi"); setTimeoutPromisified(3000).then(function () { console.log("hello"); setTimeoutPromisified(5000).then(function () { console.log("hello there"); }); });});Alt solution
setTimeoutPromisified(1000) .then(function () { console.log("hi"); return setTimeoutPromisified(3000); }) .then(function () { console.log("hello"); return setTimeoutPromisified(5000); }) .then(function () { console.log("hello there"); });