2

I have always wondered about this and have never found a convincing answer.

Please consider the following case:

var toAddress = '';
if(j==1)
{
  toAddress="[email protected]";
}
else
{
  toAddress="[email protected]";
}

sendAlertEmail(toAddress);

Can I be certain that by the time my sendAlertEmail() function is called, I will have 'toAddress' populated?

4
  • 1
    Is there anything indicating otherwise that may not be included in the code block above? Commented Mar 16, 2013 at 17:08
  • The code I have included is a simplified version of what I am trying to do. My vague recollection is at times, I have seen statements at the same hierarchy level being executed out of order. Can that happen? If yes, under what circumstances? Commented Mar 16, 2013 at 17:24
  • 2
    For the specific code above yes you can be certain. However, I suspect you are having problems getting used to async execution (indeed this question hints that you're not even aware of such a thing). So other pieces of code may be tripping you. The general rule is, if a function accepts another function as argument then check very carefully if it is asynchronous or not. For code like the above that does not have functions accepting functions as arguments then you shouldn't need to worry. Commented Mar 16, 2013 at 17:41
  • 1
    Actually, I think the question is being asked because he/she is indeed aware that async execution is occurring but unsure of when or if it always applies. It's a valid question that doesn't require a belittling answer in my humble opinion. Commented Mar 16, 2013 at 20:12

3 Answers 3

3

For code like the sample you provided:

var toAddress = '';
if(j==1)
{
  toAddress="[email protected]";
}
else
{
  toAddress="[email protected]";
}

sendAlertEmail(toAddress);

You can definitely be certain that it is strictly sequential. That is to say that the value of toAddress is either "[email protected]" or "[email protected]".

But, for code like the following:

var toAddress = '';
doSomething(function(){
  if(j==1)
  {
    toAddress="[email protected]";
  }
  else
  {
    toAddress="[email protected]";
  }
});

sendAlertEmail(toAddress);

Then it depends on whether the function doSomething is asynchronous or not. The best place to find out is the documentation. The second best is looking at the implementation.

If doSomething is not asynchronous then the code execution is basically sequential and you can definitely be certain that toAddress is properly populated.

However, if doSomething is asynchronous then you can generally be certain that the code execution is NOT sequential. Since that is one of the basic behavior of asynchronous functions - that they return immediately and execute the functions passed to them at a later time.

Not all functions that operate on functions are asynchronous. An example of synchronous function is the forEach method of arrays. But all asynchronous functions accept functions as arguments. That's because it's the only way to have some piece of code executed at the end of the asynchronous operation. So whenever you see functions taking functions as arguments you should check if it's asynchronous or not.

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

Comments

0

Node.js is single threaded (or at least the JS execution is) so since all the above code is synchronous and lined up to all occur during the same tick it will run in order and thus toAddress must be populated.

Things get complicated once you introduce an asynchronous function. In the asynchronous case it it possible for variable to shift between lines, since ticks occur between them.

To clarify during each tick the code is simply evaluated from the top of the execution to the bottom. During the first tick the scope of execution is the whole file, but after that it's callbacks and handlers.

Comments

0

The code that you wrote was pretty simple to point out the asynchronous behavior. Take a look at this code :

var toAddress = '[email protected]';
if(j==1)
{ func1(toAddress); }
else
{ func2(toAddress); }
sendAlertEmail(toAddress);

There is no guarantee that sendAlertEmail will execute only after func1 or func2 (the if else conditional) has been executed. In node functions return immediately when they are called and execute the next function called. If you want to make sure they execute sequentially use callbacks or use a library like async.

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.