0

How to run sequence functions in JavaScript: I've 4 functions like this:

function myfunction()
{
   callfunction1();
   callfunction2();
}

In callfunction1:

function callfunction1()
{
    callfunction3();
}

My problem is in "myfunction" when I call "callfunction1",callfunction1 is run and it call "callfunction3" but when "callfunction3" haven't finish,"callfunction2" is running.Have anyway to wait "callfunction3" finish then "callfunction2" is run ?

1

1 Answer 1

3

JavaScript is single-threaded, callfunction2 will only run after callfunction3 has compeletely finished.

However, if callfunction3 contains some AJAX or other asynchronous operation, then that will always happen after callfunction2 (because of the single-threaded thing, it can't start the asynchronous callback until after the synchronous stuff is done). Anything that relies on the result of an asynchronous function MUST either be in the function itself, or called by said function.

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

2 Comments

"called by said function",can you explain it to me ?
The call to callfunction2 must be inside the asynchronous callback.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.