0

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?

2 Answers 2

3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

That's it! Thanks a lot :-)
0

You have to wrap your call3 into the function:

call2().then(()=>call3()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.