- #1
shivajikobardan
- 674
- 54
- TL;DR Summary
- asynchronous javascript
https://www.physicsforums.com/threa...-as-well-as-async-await.1048282/#post-6833326
My goal is to convert the callback hell in question to using promises.
The correct flow of program is:
Order created
Order received
Preparing food
Order ready
order delivered
The below code is wrong. If you put 9000 instead of 1000, the flow isn't correct.
But I fail to understand why it's wrong. Let's do a dry run of it:
Assume 9000 instead of 1000.
Wait 9s till the promise is resolved then "order created" gets printed.
Wait 2s till the promise is resolved then "order received" gets printed.
Wait 3s till the promise is resolved then "preparing food" gets printed.
Wait 4s till the promise is resolved then "order ready" gets printed.
Wait 5s till the promise is resolved then "order ready" gets printed.
I did it using promises hell and it's correct
Dry run:
1) Wait 9s till promise is resolved, then print "order created".
then
2) Wait 1s till promise is resolved, then print "order received".
then
3) Wait 1s till promise is resolved, then print "preparing food".
then
4) Wait 1s till promise is resolved, then print "order ready".
then
5) Wait 1s till promise is resolved, then print "order delivered".
I must say I kinda get why this works (The "then".."then".."then".."then" makes it clear). But I don't understand why the first code block doesn't work.
I'm really finding it tough asynchronous javascript. I'm really out of ideas, and only a good analogy to learn can save me now.
My goal is to convert the callback hell in question to using promises.
The correct flow of program is:
Order created
Order received
Preparing food
Order ready
order delivered
The below code is wrong. If you put 9000 instead of 1000, the flow isn't correct.
JavaScript:
function createorder(timer = 0) {
return new Promise(function (resolve) {
setTimeout(resolve, timer);
});
}
createorder(1000).then(() => {
console.log('Order Created');
});
createorder(2000).then(() => {
console.log('Order Received');
});
createorder(3000).then(() => {
console.log('Preparing Food');
});
createorder(4000).then(() => {
console.log('Order Ready');
});
createorder(5000).then(() => {
console.log('Order Delivered');
});
But I fail to understand why it's wrong. Let's do a dry run of it:
Assume 9000 instead of 1000.
Wait 9s till the promise is resolved then "order created" gets printed.
Wait 2s till the promise is resolved then "order received" gets printed.
Wait 3s till the promise is resolved then "preparing food" gets printed.
Wait 4s till the promise is resolved then "order ready" gets printed.
Wait 5s till the promise is resolved then "order ready" gets printed.
I did it using promises hell and it's correct
JavaScript:
function createOrder(timer)
{
return new Promise(function(resolve,reject){
setTimeout(resolve,timer);
})
}
createOrder(9000).then(function(){
console.log("Order created");
createOrder(1000).then(function(){
console.log("order received");
createOrder(1000).then(function(){
console.log("preparing food");
createOrder(1000).then(function(){
console.log("order ready");
createOrder(1000).then(function(){
console.log("order delivered");
})
})
})
})
})
Dry run:
1) Wait 9s till promise is resolved, then print "order created".
then
2) Wait 1s till promise is resolved, then print "order received".
then
3) Wait 1s till promise is resolved, then print "preparing food".
then
4) Wait 1s till promise is resolved, then print "order ready".
then
5) Wait 1s till promise is resolved, then print "order delivered".
I must say I kinda get why this works (The "then".."then".."then".."then" makes it clear). But I don't understand why the first code block doesn't work.
I'm really finding it tough asynchronous javascript. I'm really out of ideas, and only a good analogy to learn can save me now.