0

Using the Visual representation from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/EventLoop:

Would one of the differences between setImmediate() and nextTick() be that setImmediate() places the code in the stack(current running code) versus nextTick() places the code in the next message(queue)?

enter image description here

dman
  • 10,406
  • 18
  • 102
  • 201
  • are you talking about `node.js` runtime? Also, is this a dup: http://stackoverflow.com/a/15349865/798682 ? – mattr Nov 06 '14 at 17:26

1 Answers1

1

No. Both these methods are asynchronous, which means that the callback goes onto the queue and the stack is left alone (unlike as if it was called right away).

However, setImmediate and nextTick do use different queues. There are different execution cycles for them, and nextTick callbacks would all be invoked before the first setImmediate-scheduled one is. See also setImmediate vs. nextTick for details.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • please explain you mean by that they use different queues. As far as I know, the event loop only has one queue that these api's work on. – dman Nov 06 '14 at 19:07
  • Well, no. There are [multiple event loops](http://stackoverflow.com/questions/10680601/nodejs-event-loop), which you could think of "running inside of each other" (technically, it might as well be a single queue but with different insertion methods). As detailed in the linked question, `nextTick` callbacks will run before IO callbacks. – Bergi Nov 06 '14 at 19:25