0

I've seen quite a few questions about exection order issues in JavaScript involving ajax calls.

If there are no ajax calls, is it safe to assume that code will execute from top to bottom?

Say, I have this code below. None of the functions that I'm calling have any async calls in them.

someFunction() {

   // Call some external function
   doSomething();

   // Then call another external function
   const x = doSomethingElse();

   if(x === 0) {

      // Call third function
      thirdFunction();
   }

}

Currently, I'm running into a situation where it appears as if doSomething() is not getting called first. Is this possible?

P.S. I'm having this issue in a React/Redux app and doSomething() is an action that is supposed to reset/clear something in the store but currently that's not working.

3
  • did you debug that if your cursor goes inside dosomething() or not? Commented Sep 7, 2017 at 5:13
  • Is something else calling it? Is it getting called before the data arrives, and then again when it does? Commented Sep 7, 2017 at 5:35
  • When I step through the code, I do hit doSomething() first. So the order seems OK but for some reason, the code is not hitting the reducer. So, it's a different issue. Thanks! Commented Sep 7, 2017 at 5:40

2 Answers 2

1

Along with ajax calls promises that don't resolve (for example event listeners waiting for an event that never happens) and timeout code that gets pushed to next event cycle are also async.

If doSomething() has any one of these then it might not get executed.

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

Comments

0

Yes. If you don't have any function which has asynchronous calls like AJAX, execution order is always the same as it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.