2

In Node.js I have code similar to the following:

function requestHandler() {
    expression1; // Not I/O related
    expression2;
}

The question I have is if it is possible that expression1 gets executed for request 1 and then expression1 for request 2. I need to make sure that expression1 and expression2 run for one request before expression1 gets executed for any other request. Is this possible in Node.js?

Robert Hickman
  • 997
  • 1
  • 11
  • 22
  • I'm wondering if this thread answers my question: http://stackoverflow.com/questions/5481675/node-js-and-mutexes It says "Events are handled the moment there's no other code to run." If that is true, then I don't need to worry about mutual exclusion because then each handled event is mutually exclusive already. – Robert Hickman Nov 20 '15 at 04:38
  • 2
    It really depends on if `expression1` synchronous, or if it is asynchronous. If `expression1` applies `setTimeout` or `process.nextTick` it is defferring execution. So, the answer depends on what `expression1` really is. – S.D. Nov 20 '15 at 04:57
  • @S.D. I think that answers my question then. So node will continue to process a particular block of code until it either 1) finishes OR 2) defers processing with setTimeout/setInterval or process.nextTick? What about setImmediate? – Robert Hickman Nov 20 '15 at 15:26
  • That too "schedules" instead of running the callback at place. – S.D. Nov 21 '15 at 09:14

0 Answers0