diff options
author | Alex Early <alexander.early@gmail.com> | 2016-03-08 15:14:44 -0800 |
---|---|---|
committer | Alex Early <alexander.early@gmail.com> | 2016-03-08 15:14:44 -0800 |
commit | f6a19cec4dae3afd3dfb53d07baabd88ce282b5c (patch) | |
tree | 8a08bdd1d4f0c50baecd902220b477b3b999200b | |
parent | f51042c5510b4977a24a022a0bce729ffdc8d452 (diff) | |
parent | 1ebbcd722605476c3b9c9476a231baa76ed8c45f (diff) | |
download | async-f6a19cec4dae3afd3dfb53d07baabd88ce282b5c.tar.gz |
Merge pull request #1053 from caolan/setimmediate-args
Pass extra args to setImmediate/nextTick.
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | lib/internal/setImmediate.js | 18 | ||||
-rw-r--r-- | lib/nextTick.js | 4 | ||||
-rw-r--r-- | mocha_test/nextTick.js | 38 | ||||
-rw-r--r-- | mocha_test/setImmediate.js | 24 | ||||
-rwxr-xr-x | test/test-async.js | 27 |
6 files changed, 79 insertions, 39 deletions
@@ -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)` @@ -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__ @@ -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) ``` --------------------------------------- diff --git a/lib/internal/setImmediate.js b/lib/internal/setImmediate.js index c02ad71..7d15249 100644 --- a/lib/internal/setImmediate.js +++ b/lib/internal/setImmediate.js @@ -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); + }); +}); diff --git a/lib/nextTick.js b/lib/nextTick.js index b61a8b4..05d9555 100644 --- a/lib/nextTick.js +++ b/lib/nextTick.js @@ -2,6 +2,4 @@ import setImmediate from './internal/setImmediate'; -var nexTick = typeof process === 'object' && typeof process.nextTick === 'function' ? process.nextTick : setImmediate; - -export default nexTick; +export default setImmediate; diff --git a/mocha_test/nextTick.js b/mocha_test/nextTick.js new file mode 100644 index 0000000..b428822 --- /dev/null +++ b/mocha_test/nextTick.js @@ -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); + }); +}); diff --git a/mocha_test/setImmediate.js b/mocha_test/setImmediate.js new file mode 100644 index 0000000..854111a --- /dev/null +++ b/mocha_test/setImmediate.js @@ -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); + }); + +}); diff --git a/test/test-async.js b/test/test-async.js index 85e9f0e..5643418 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1894,33 +1894,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); |