Sequence of code execution in nodejs app?

I have always wondered about this and have never found a convincing answer. May be you guys can help clear it up.

please consider the following case:

case 1:

var toAddress = '';
if(j==1)
{
  toAddress="abc@mydomain.com";
}
else
{
  toAddress="xyz@mydomain.com";
}

sendAlertEmail(toAddress);

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

Answers/clarifications much appreciated!

--su

For code like the sample you provided:

var toAddress = '';
if(j==1)
{
  toAddress="abc@mydomain.com";
}
else
{
  toAddress="xyz@mydomain.com";
}

sendAlertEmail(toAddress);

You can definitely be certain that it is strictly sequential. That is to say that the value of toAddress is either "abc@mydomain.com" or "xyz@mydomain.com".

But, for code like the following:

var toAddress = '';
doSomething(function(){
  if(j==1)
  {
    toAddress="abc@mydomain.com";
  }
  else
  {
    toAddress="xyz@mydomain.com";
  }
});

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.

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.

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

var toAddress = 'abc@mydomain.com';
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.