0

I am using websocket libarary for creating some sockets to pass data to my application.some where in my code i wrote following line.

 connection.on('message', function(message) 
  {
     SetOfInstruction1;  
     SetOfInstruction2;   
  });

My doubt is : Is SetOfInstruction1 and SetOfInstruction2; execute in sequence or they execute in a Asynch manner(it means setOfInstruction2 can excute before setOfInstruction1) ????

following are example of SetOfInstruction1 and SetOfInstruction2:

Example SetOfInstruction1

for (i 1 to n)
{
     find something1 in something2 array
     if something1 found then set found boolean to true else false;
}

Example for SetOfInstruction2 is

for (i:1 to n)
{
  if found =true; and someOtherConditions matches
  send message back  with true response
  else{
    //continue 
}


 }

2 Answers 2

2
SetOfInstruction1;
SetOfInstruction2;

These instructions will always execute in this order. Javascript doesn't randomly mix up execution order of individual lines. Now...

someFunc(function () { console.log('first'); });
someFunk(function () { console.log('second'); });

Here it's entirely unpredictable which will be logged first. It depends on whether someFunc and/or someFunk are asynchronous or synchronous, and if they're asynchronous which one will finish first. Again, we can absolutely assert that someFunc will be called before someFunk, but the execution order of the callbacks depends on the innards of those functions.

There are synchronous functions which take callbacks, like this:

arr.sort(function (a, b) { return a - b; });
console.log(arr);

Here execution order is what you see. The console will log the sorted array. But there are also asynchronous functions which take callbacks:

setTimeout(function () { console.log('second'); });
console.log('first');

The callback will be called sometime after the console logged 'first'.

You'll have to read the function's documentation to know which is which.

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

2 Comments

thanx @deceze ,my setOfInstructions1 are just some forloops and in that forloops ,i am just finding something and assigning a boolean (say X) accordingly and then in next setOfInstructions are again some forloops in that according to previous forloop boolean value X i am performing some task. do they execute in sequence?
can only set one answer to solution :)
1

it is up to the functions you are invoking. you need to read the documentation and see what functions are asynchrounos and what are not.

for example fs.readdir will be called in an async manner, but fs.readdirSync will not.

that has been said, usually "regular" JS actions like traversing an array, setting a variables some value etc. will not be called asynchronosly.

about node.js specific api's - read the docs.

3 Comments

Hey David,have you ever find a link where there is proper listing of sync functions and asynch functions?
the official node.js documentation here: nodejs.org/api , plus questions here in stack overflow if the documentation doesn't specifiy.
thanx david,its ver helping

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.