Skip to content

Commit

Permalink
pass extra args to setImmediate/nextTick. Fixes #940
Browse files Browse the repository at this point in the history
  • Loading branch information
aearly committed Mar 8, 2016
1 parent 0600652 commit 0736189
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 36 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ three
---------------------------------------
<a name="nextTick"></a>
### nextTick(callback), setImmediate(callback)
### nextTick(callback, [args...]), setImmediate(callback, [args...])
Calls `callback` on a later loop around the event loop. In Node.js this just
calls `process.nextTick`; in the browser it falls back to `setImmediate(callback)`
Expand All @@ -1580,6 +1580,7 @@ This is used internally for browser-compatibility purposes.
__Arguments__
* `callback` - The function to call on a later loop around the event loop.
* `args...` - any number of additional arguments to pass to the callback on the next tick
__Example__
Expand All @@ -1590,6 +1591,10 @@ async.nextTick(function(){
// call_order now equals ['one','two']
});
call_order.push('one')

async.setImmediate(function (a, b, c) {
// a, b, and c equal 1, 2, and 3
}, 1, 2, 3)
```
---------------------------------------
Expand Down
18 changes: 10 additions & 8 deletions lib/internal/setImmediate.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict';
import rest from 'lodash/rest';

var _setImmediate = typeof setImmediate === 'function' && setImmediate;

var _delay;
var _defer;
if (_setImmediate) {
_delay = function(fn) {
// not a direct alias for IE10 compatibility
_setImmediate(fn);
};
_defer = _setImmediate;
} else if (typeof process === 'object' && typeof process.nextTick === 'function') {
_delay = process.nextTick;
_defer = process.nextTick;
} else {
_delay = function(fn) {
_defer = function(fn) {
setTimeout(fn, 0);
};
}

export default _delay;
export default rest(function (fn, args) {
_defer(function () {
fn.apply(null, args);
});
});
38 changes: 38 additions & 0 deletions mocha_test/nextTick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var async = require('../lib');
var expect = require('chai').expect;

describe("nextTick", function () {

it('basics', function(done){
var call_order = [];
async.nextTick(function(){call_order.push('two');});
call_order.push('one');
setTimeout(function(){
expect(call_order).to.eql(['one','two']);
done();
}, 50);
});

it('nextTick in the browser', function(done){
if (!process.browser) {
// skip this test in node
return done();
}

var call_order = [];
async.nextTick(function(){call_order.push('two');});

call_order.push('one');
setTimeout(function(){
expect(call_order).to.eql(['one','two']);
done();
}, 50);
});

it("extra args", function (done) {
async.nextTick(function (a, b, c) {
expect([a, b, c]).to.eql([1, 2, 3]);
done();
}, 1, 2, 3);
});
});
24 changes: 24 additions & 0 deletions mocha_test/setImmediate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var async = require('../lib');
var expect = require('chai').expect;

describe("setImmediate", function () {

it('basics', function(done){
var call_order = [];
async.setImmediate(function(){call_order.push('two');});
call_order.push('one');

setTimeout(function(){
expect(call_order).to.eql(['one','two']);
done();
}, 25);
});

it("extra args", function (done) {
async.setImmediate(function (a, b, c) {
expect([a, b, c]).to.eql([1, 2, 3]);
done();
}, 1, 2, 3);
});

});
27 changes: 0 additions & 27 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2042,33 +2042,6 @@ console_fn_tests('dir');
console_fn_tests('warn');
console_fn_tests('error');*/

exports['nextTick'] = function(test){
test.expect(1);
var call_order = [];
async.nextTick(function(){call_order.push('two');});
call_order.push('one');
setTimeout(function(){
test.same(call_order, ['one','two']);
test.done();
}, 50);
};

exports['nextTick in the browser'] = function(test){
if (!isBrowser()) {
// skip this test in node
return test.done();
}
test.expect(1);

var call_order = [];
async.nextTick(function(){call_order.push('two');});

call_order.push('one');
setTimeout(function(){
test.same(call_order, ['one','two']);
}, 50);
setTimeout(test.done, 100);
};

exports['concat'] = function(test){
test.expect(3);
Expand Down

0 comments on commit 0736189

Please sign in to comment.