mafintosh / mutexify

Bike shed mutex lock implementation
MIT License
88 stars 10 forks source link

use setImmediate instead of process.nextTick #4

Closed Kikobeats closed 9 years ago

Kikobeats commented 9 years ago

Because process.nextTick is deprecated:

However, there are programs out in the wild that use recursive calls to process.nextTick to avoid pre-empting the I/O event loop for long-running jobs. In order to avoid breaking horribly right away, Node will now print a deprecation warning, and ask you to use setImmediate for these kinds of tasks instead.

http://blog.nodejs.org/2013/03/11/node-v0-10-0-stable/

mafintosh commented 9 years ago

setImmediate is only needed if you rely on io happening in between. We don't. Therefore nextTick is fine in our case :) You'll notice the nextTick deprecation warning is only printed on recursive nextTick calls for this reason.

Kikobeats commented 9 years ago

oh, understood. Can you put an example where is better use setImmediate instead of process.nextTick. Now, I appreciate more the difference but still is difficult to me know the scenarios where use each.

mafintosh commented 9 years ago

I haven't really ever had a use case for setImmediate but I asked out on twitter, https://twitter.com/mafintosh/status/624590818125352960

Kikobeats commented 9 years ago

Thanks!!

I have a bonus question related with this, and it's about what happens when you try to execute synchronous code as asynchronous code. In async library the recommendation is envolved the fake async code using nextTick/setImmediate:

async.eachSeries(hugeArray, function iterator(item, callback) {
  if (inCache(item)) {
    async.setImmediate(function () {
      callback(null, cache[item]);
    });
  } else {
    doSomeIO(item, callback);
  //...

This is because how async internally works, or, in fact, whatever fake sync code as async must be involved following the example?