Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow passing optional arguments to process.nextTick. #736

Closed
wants to merge 1 commit into from

7 participants

@tobie

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.

@TooTallNate
Owner

+1 for eliminating another anonymous scope

@koichik
Owner

how about Function.prototype.bind()?

process.nextTick(callback.bind(null, null, someValue));
@koichik
Owner

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

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

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

@koichik what is the status on this?

@koichik
Owner

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

@betamos

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

@tobie

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

@bnoordhuis

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.

@trevnorris
Owner

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.

@TooTallNate
Owner

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

@trevnorris
Owner

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

@bnoordhuis

Let's close this.

@bnoordhuis bnoordhuis closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
8 doc/api/process.markdown
@@ -296,11 +296,11 @@ This will generate:
`heapTotal` and `heapUsed` refer to V8's memory usage.
-### process.nextTick(callback)
+### process.nextTick(callback, [args], [...])
-On the next loop around the event loop call this callback.
-This is *not* a simple alias to `setTimeout(fn, 0)`, it's much more
-efficient.
+On the next loop around the event loop call this callback. Optionally, you
+can also pass arguments to the callback. This is *not* a simple alias to
+`setTimeout(fn, 0)`, it's much more efficient.
process.nextTick(function () {
console.log('nextTick callback');
View
11 src/node.js
@@ -119,8 +119,17 @@
nextTickQueue.splice(0, l);
};
+ var pSlice = Array.prototype.slice;
+
process.nextTick = function(callback) {
- nextTickQueue.push(callback);
+ if (arguments.length == 1) {
+ nextTickQueue.push(callback);
+ } else {
+ var args = pSlice.call(arguments, 1);
+ nextTickQueue.push(function() {
+ callback.apply(null, args);
+ });
+ }
process._needTickCallback();
};
};
View
18 test/simple/test-next-tick-extra-args.js
@@ -0,0 +1,18 @@
+var common = require('../common');
+var assert = require('assert');
+
+var argumentsLength, firstArg, secondArg;
+
+function callback() {
+ argumentsLength = arguments.length;
+ firstArg = arguments[0];
+ secondArg = arguments[1];
+}
+
+process.nextTick(callback, 123, 456);
+
+process.addListener('exit', function() {
+ assert.equal(2, argumentsLength);
+ assert.equal(123, firstArg);
+ assert.equal(456, secondArg);
+});
Something went wrong with that request. Please try again.