- #1
shivajikobardan
- 674
- 54
- TL;DR Summary
- callback hell rewriting using promises as well as async/await
JavaScript:
function createOrder() {
setTimeout(function() {
console.log("order created");
setTimeout(function() {
console.log("order received");
setTimeout(function() {
console.log("preparing food");
setTimeout(function() {
console.log("order ready");
setTimeout(function() {
console.log("order delivered");
}, 6000)
}, 4000)
}, 5000)
}, 1000)
}, 2000)
}
createOrder();
https://learnwithtriveni.com/2022/08/19/callback-hell-in-javascript/
I want to rewrite it using promises and async awaits.
I've indeed done it with promises and async, as shown here:
JavaScript:
async function createOrder() {
setTimeout(() => {
console.log("order created");
}, 1000)
}
createOrder().then(
setTimeout(() => {
console.log("order received");
}, 2000))
.then(
setTimeout(() => {
console.log("preparing food");
}, 3000)
)
.then(
setTimeout(() => {
console.log("order ready");
}, 4000)
)
.then(
setTimeout(() => {
console.log("order delivered");
}, 5000)
)
I know the full syntax of async/await and promises as well.
I'll share if required.
Can you share the code for it? I'm really confused. It'd be really helpful to see and feel the code.
https://www.freecodecamp.org/news/h...llbacks-and-avoid-callback-hell-1bc8dc4a2012/
I've read this article but since I don't know much about burgers(or maybe this article wasn't a great fit for me anyways), this article was so difficult to read for me and I ended up learning nothing.