Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Allow passing optional arguments to process.nextTick. #736

wants to merge 1 commit into from

7 participants


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


how about Function.prototype.bind()?

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

bind() is useful for even a method.

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

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).


@koichik what is the status on this?


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


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


Shouldn't node simply provide a setImmediate function now? See:


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,, 1));
    } else {

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?


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.
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
+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');
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 =, 1);
+ nextTickQueue.push(function() {
+ callback.apply(null, args);
+ });
+ }
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.