[Pkg-javascript-commits] [node-async] 34/480: exposed browser-compatible nextTick function

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-async.

commit 19d57ef57fd018642d28b2b514fff8357cdbc3f0
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Mon Aug 2 21:38:44 2010 +0100

    exposed browser-compatible nextTick function
---
 README.md          | 24 ++++++++++++++++++++++++
 lib/async.js       | 19 ++++++++-----------
 test.js            |  6 +++---
 test/test-async.js | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index c08cf8b..d310e32 100644
--- a/README.md
+++ b/README.md
@@ -125,6 +125,9 @@ apply
 : Creates a continuation with some arguments already applied, a useful
   shorthand when combined with other flow control functions.
 
+nextTick
+: Calls the callback on a later loop around the event loop.
+
 ### Utils
 
 log
@@ -638,6 +641,27 @@ continuation:
     two
     three
 
+### nextTick(callback)
+
+Calls the callback on a later loop around the event loop. In node.js this just
+calls process.nextTick, in the browser it falls back to setTimeout(callback, 0),
+which means other higher priority events may precede the execution of the callback.
+
+This is used internally for browser-compatibility purposes.
+
+__Arguments__
+
+* callback - The function to call on a later loop around the event loop.
+
+__Example__
+
+    var call_order = [];
+    async.nextTick(function(){
+        call_order.push('two');
+        // call_order now equals ['one','two]
+    });
+    call_order.push('one')
+
 
 ## Utils
 
diff --git a/lib/async.js b/lib/async.js
index b4ba24b..1541b73 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -43,18 +43,15 @@
         return -1;
     };
 
-    //// nextTick implementation with browser-compatible fallback ////
+    //// exported async module functions ////
 
-    var _nextTick;
-    if(typeof process === 'undefined' || !process.nextTick){
-        _nextTick = function(fn){
+    //// nextTick implementation with browser-compatible fallback ////
+    exports.nextTick = function(fn){
+        if(typeof process == 'undefined' || !(process.nextTick)){
             setTimeout(fn, 0);
-        };
-    }
-    else _nextTick = process.nextTick;
-
-
-    //// exported async module functions ////
+        }
+        else process.nextTick(fn);
+    };
 
     exports.forEach = function(arr, iterator, callback){
         if(!arr.length) return callback();
@@ -329,7 +326,7 @@
                     var next = iterator.next();
                     if(next) args.push(wrapIterator(next));
                     else     args.push(callback);
-                    _nextTick(function(){iterator.apply(null, args);});
+                    exports.nextTick(function(){iterator.apply(null, args);});
                 }
             };
         };
diff --git a/test.js b/test.js
index f41d154..423cd0e 100755
--- a/test.js
+++ b/test.js
@@ -1,8 +1,8 @@
 #!/usr/local/bin/node
 
-require.paths.push(__dirname);
-require.paths.push(__dirname + '/deps');
-require.paths.push(__dirname + '/lib');
+require.paths.unshift(__dirname);
+require.paths.unshift(__dirname + '/deps');
+require.paths.unshift(__dirname + '/lib');
 
 try {
     var testrunner = require('nodeunit').testrunner;
diff --git a/test/test-async.js b/test/test-async.js
index 312a81f..e844c54 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -804,3 +804,40 @@ console_fn_tests('dir');
 /*console_fn_tests('info');
 console_fn_tests('warn');
 console_fn_tests('error');*/
+
+exports['nextTick'] = function(test){
+    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 node'] = function(test){
+    test.expect(1);
+    var _nextTick = process.nextTick;
+    process.nextTick = function(){
+        process.nextTick = _nextTick;
+        test.ok(true, 'process.nextTick called');
+        test.done();
+    };
+    async.nextTick(function(){});
+};
+
+exports['nextTick in the browser'] = function(test){
+    test.expect(1);
+    var _nextTick = process.nextTick;
+    process.nextTick = undefined;
+
+    var call_order = [];
+    async.nextTick(function(){call_order.push('two');});
+
+    call_order.push('one');
+    setTimeout(function(){
+        process.nextTick = _nextTick;
+        test.same(call_order, ['one','two']);
+    }, 50);
+    setTimeout(test.done, 100);
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git



More information about the Pkg-javascript-commits mailing list