Reputation: 467
I am trying to call functions call2() and then call3() in this order, so that call3() is called only when call2() terminates. I am using Promise to achieve that.
But call3() is called before call2() terminates. Here is my code:
function call2() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("calling 2");
resolve(true);
}, 3000);
});
}
function call3() {
console.log("calling 3");
}
call2().then(call3());
I am obviously doing something wrong, or could not understand how to use promise. Any help please?
Upvotes: 0
Views: 42
Reputation: 11
You have to wrap your call3 into the function:
call2().then(()=>call3());
Upvotes: 0
Reputation: 2852
In then(call3())
you are calling the call3 function rather than passing it as a callback, change to this:
call2().then(call3);
function call2() {
console.log('Start...');
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("calling 2");
resolve(true);
}, 3000);
});
}
function call3() {
console.log("calling 3");
}
call2().then(call3);
Upvotes: 3