1

The impression I get from people is... All JavaScript functions are synchronous unless used with process.nextTick. When's the best time to use it?

I want to make sure that I don't over use it in places where I don't need it. At this point, I'm thinking to use it right before something like a database call, however, at the same time, as I understand, those calls are asynchronous by default because of the whole "async IO" thing.

Are they to be used only when doing some intensive work within the JavaScript boundaries? Like parsing XML etc?

Btw, there's already a question like this but it seems dead so I raised another one.

Manthan Dave
  • 153
  • 1
  • 11
  • `functions are synchronous unless used with process.nextTick` nope, they are asynchronous when you use callbacks instead of return, and when you must wait for something. `process.nextTick` is used to push the code to the event loop, and allow it to clean up (example: recursive loop of async function, which would lead to call stack overflow) – DrakaSAN Oct 24 '16 at 08:14
  • 1
    @DrakaSAN Yeah, but actually ["using callbacks" is not exactly a sign of asynchrony](http://stackoverflow.com/q/19083357/1048572) either. – Bergi Oct 24 '16 at 10:34

2 Answers2

4

I'm thinking to use it right before something like a database call, however, at the same time, as I understand, those calls are asynchronous by default because of the whole "async IO" thing.

Yes. The database driver itself should be natively asynchronous already, so you don't need to use process.nextTick yourself here to "make it asynchronous". The most time-consuming part is the IO and the computations inside the database, so waiting an extra tick just slows things down actually.

Are they to be used only when doing some intensive work within the JavaScript boundaries? Like parsing XML etc?

Yes, exactly. You can use it to prevent large synchronous functions from blocking your application. If you want to parse an XML file, instead of gnawing through it for 3 seconds during which no new connections can be opened, no requests received, and no responses be sent, you would stream the file and parse only small chunks of it every time before using nextTick and allowing other work to be done concurrently.

However, notice that the parser should use nextTick internally and offer an asynchronous API, instead of the caller using nextTick before invoking the parser.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
3

This answer makes no claims of being complete, but here are my thoughts:

I can imagine two use cases. The first one is, to make sure something is really async. This comes in handy when using EventEmitter. Imagine you want to be able to use all methods of your emitter like this:

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {
  aMethod(){
    console.log('some sync stuff');
    this.emit('aMethodResponse');
    return this;
  }
}
var myEmitter = new MyEmitter();
myEmitter.aMethod()
  .once('aMethodResponse', () => console.log('got response'));

This will simply not work as the event is fired before the listener is established. process.nextTick() makes sure that this won't happen.

aMethod(){
  console.log('some sync stuff');
  process.nextTick(() => this.emit('aMethodResponse'));
  return this;
}

Edit: removed second suggestion because it was simply wrong

Johannes Merz
  • 3,252
  • 17
  • 33
  • Good point but I thought to make it run after something, you had to run `process.setImmediate` to queue the call after IO function which would set it further behind? Also, is there an established pattern for wrapping functions with `process.nextTick` or `process.setImmediate`? – Manthan Dave Oct 24 '16 at 08:53
  • my bad, removed the second part. The first one still stands though. – Johannes Merz Oct 24 '16 at 09:14