1

I know that node.js is asynchronous, but what I don't really understand is how much.

Example:

If a need to do 3 things in sequence, and I need every thing is done before the other begins, Do i must need to use Callback?

var myContainer;
for(var i=0;i<10000;i++)
    myContainer.push(i.toString());
for(var j=0;j<myContainer.length;j++)
    console.log(myContainer[j]);
for(var x=0;x<myContainer.length;x++)
    myModuleForEmails.sendEmailsTo(myContainer[x]);

Ok, suppose for one moment I have a module like Imap ready and calling myModuleForEmails.sendEmailsTo(myContainer[x]) I really send the email.

I know also that this program is absolutely useless, but it's just to understand.

Suppose I must push all 10000 string in myContainer, only THEN log all the string that are in myContainer in the console, and only AFTER BOTH I need to send the emails.

Is this version reliable or do I need 2 callback? And Does the number of iteration I do matter? i.e, if i had used 10 instead of 10000, could I have used this syntax because it takes so few to do 10 operation that finishes the first for cycle before starting the second?

5
  • 1
    This should help stackoverflow.com/questions/25173808/node-js-parallel-execution Commented Aug 19, 2014 at 8:54
  • 3
    Nothing in NodeJS (and more generally in JavaScript) is asynchronous except certain specific functions (on "host objects") and modules/other functions that use them. Stuff like for loops etc is all synchronous. Commented Aug 19, 2014 at 8:54
  • 1
    Javascript isn't asynchronous at all. Certain host functions may involve an asynchronous operation(conducted outside of the javascript runtime), which may at some point queue a call to one of your javascript functions(which will be executed synchronously) Commented Aug 19, 2014 at 8:55
  • Synchronous functions are synchronous. Asynchronous are asynchronous. Providing an exhaustive list of the two would be off-topic for Stackoverflow. The documentation should tell you if a function is asynchronous. Commented Aug 19, 2014 at 8:58
  • When you read the documentation it will tell you what is, and isn't async. In your example the last line might be the only operation that is async because input ouput is what node does async. And why is there another Quentin commenting. I thought I was unique. Commented Aug 19, 2014 at 9:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.