I have been learning how to use callback with stupid callback hell.
This is an example I have created: https://jsfiddle.net/1donpcvv/
It is working as expected. step1()
has to complete first and then move on to step2()
and then finally step3()
I am trying to convert to promise which I am struggling to get it work. What did I do wrong?
JS:
function step1() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("Step 1 done After 3 seconds");
resolve();
}, 3000);
});
}
function step2() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("Step 2 done After 2 seconds");
resolve();
}, 2000);
});
}
function step3() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("Step 3 done After 1 seconds");
resolve();
}, 1000);
});
}
Usage
step1().then().step2().then().step3();