0

promises ES6 probleme

hi everyone, how do you doing Please I have some issues here when I exec the code below can someone tell me what is wrong !!! ? I just try to learn promises in ES6 the result of this code is undefined and I don't know why let variable = true; function Calculate(Number1, Number2){ return new Promise(function(resolve, reject){ setTimeout(function(){ const some = Number1 + Number2; if(variable){ resolve(some); } else { reject('Sorry'); } }, 1500) }) } function promptAlert(number){ return new Promise(function(resolve, reject){ setTimeout(function(){ number += 2; resolve(number); }, 1000); }) } Calculate(5,5) .then((some) => { promptAlert(some); }) .then((num) => { alert(num); })

16th Jul 2021, 9:10 PM
Youssef Elouasbi
Youssef Elouasbi - avatar
3 Respuestas
+ 2
return promptAlert ,otherwise it will operate on the previous promise object.
16th Jul 2021, 9:45 PM
Abhay
Abhay - avatar
+ 2
Instead of using an anonymous function as a call back, you can use promptAlert as the callback. The same is also true for the second then, alert can be the callback .then(promptAlert).then(alert)
16th Jul 2021, 10:08 PM
ODLNT
ODLNT - avatar
+ 1
thank you so much for your help :)
16th Jul 2021, 11:06 PM
Youssef Elouasbi
Youssef Elouasbi - avatar