0

I'm getting slightly annoyed now after reading many different articles on how to write proper code in node js.

I would just like some clarification on whether I'm correct or not with each of these statements:

  • Code is executed synchronously
  • A for loop or while loop etc. will be executed asynchronously
  • By doing the code below isn't proper asynchronous:

L

function doSomething(callback) {
    // doing some code

    // much much code

    callback();
}

The reason that people are saying that this won't work properly is that code is executed asynchronously, so the callback won't be fired off at the end of the code it will all be executed at once.

So for example if you were filling some object by doing some stuff and you wanted to post that full object back through the callback it doesn't work because it will all be executed at the same time.

2 Answers 2

2

No, for and while loops are synchronous in Node.js.

Whether your doSomething function example will work or not all depends on whether you're making any calls to asynchronous functions before calling callback(). If you're making asynchronous calls, then you'd need to postpone calling callback() until those asynchronous calls have completed. If you're only making synchronous calls, then you don't need to use a callback and your function can synchronously return the result.

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

Comments

1

Code in node.js isn't standardly asynchronous. Calls that you place one after another will still be executed one after another. A for or while loop will still block the main code in this way.

However, you can pass around functions which will be executed later, through process.nextTick(callback). This method will add a function to the event queue, which node.js takes care of internally. This will only be executed after any other blocking code has been extecuted. An example:

function doWork(callback) {
    //Do some work here, for instance a for loop that might take a while

    callback(workReslt);
}

function workDone(workResult) {
    //Process the result here
}

function startWork() {
    process.nextTick(doWork(workDone));
}

This will execute doWork() when the server passes through the event loop again. This way, the startWork() function will not block the main code.

However, most modules in node.js already implement this. Database and file access is usually non-blocking, it will simply fire a callback when it is done with it's work.

If you really have to perform heavy async calculations (or perhaps some IO that doesn't already have a module for it), you can look into the async module, which has a nice tutorial here: http://justinklemm.com/node-js-async-tutorial/

As for coding conventions, think of it this way. If you know the action is going to take time, you should make it asynchronous (either through the method I mentioned first or the async module). Actions like these are usually related to I/O, which usually also means there is already an asynchronous module for it. However, most cases I have come across up to now, have never required this.

Sources: http://howtonode.org/understanding-process-next-tick

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.