This repository has been archived by the owner. It is now read-only.

Allow passing optional arguments to process.nextTick. #736

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
7 participants

tobie commented Mar 1, 2011

This patch allows for extra, optional arguments to process.nextTick to be simply passed to the callback when it is invoked. This is modeled after setTimeout's API:

// Allows turning:

process.nextTick(function() {
  callback(null, someValue);
});

// into:

process.nextTick(callback, null, someValue);

Comes with tests and doc.

+1 for eliminating another anonymous scope

koichik commented Mar 1, 2011

how about Function.prototype.bind()?

process.nextTick(callback.bind(null, null, someValue));

koichik commented Mar 2, 2011

bind() is useful for even a method.
example:

process.nextTick(emitter.emit.bind(emitter, 'data', buffer));

tobie commented Mar 2, 2011

The syntax of bind isn't nearly as nice. Also, I haven't seen methods used as callbacks in node.js that much (if at all).

japj commented Jul 21, 2011

@koichik what is the status on this?

koichik commented Jul 23, 2011

I am -1, but not strong opinion.
If other guys want to land this, I won't block.

betamos commented Nov 23, 2011

+1, it feels node'ey and couldn't hurt

tobie commented Nov 23, 2011

Shouldn't node simply provide a setImmediate function now? See: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html.

Member

bnoordhuis commented Nov 24, 2011

Shouldn't node simply provide a setImmediate function now?

Yes, possibly. I like that name better than process.nextTick(). The downside is that the setImmediate spec is still a draft.

From the MDN docs it sounds like setImmediate will never be more than just a draft, but here's a shim:

function setImmediate(cb) {
    var ar = arguments;
    if (ar.length > 1) {
        process.nextTick(function() {
            cb.apply(null, Array.prototype.slice.call(ar, 1));
        });
    } else {
        process.nextTick(cb);
    }
};

I'm not sure if something like this should be included in node proper, or just left as a shim.

As a side note, I ran some performance tests on my machine. process.nextTick() ran in about 24000 ns. where setImmediate with two arguments ran in about 150000 ns. So it's about 6x's slower. Real time difference of about 0.15 milliseconds.

Note: I also tried the shim using Function.bind, but it was about twice as slow as the above.

@trevnorris The v0.9.x releases have a proper setImmediate() implementation (it's not just a wrapper around process.nextTick()).

@TooTallNate Cool. So can this one be closed, or want to wait for the v0.10.x release?

Member

bnoordhuis commented Oct 30, 2012

Let's close this.

@bnoordhuis bnoordhuis closed this Oct 30, 2012

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.