summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--lib/async.js15
2 files changed, 14 insertions, 6 deletions
diff --git a/README.md b/README.md
index 40b1c0f..5c3351b 100644
--- a/README.md
+++ b/README.md
@@ -1067,8 +1067,9 @@ 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.
+calls process.nextTick, in the browser it falls back to setImmediate(callback)
+if available, otherwise setTimeout(callback, 0), which means other higher priority
+events may precede the execution of the callback.
This is used internally for browser-compatibility purposes.
diff --git a/lib/async.js b/lib/async.js
index b3c25d6..4185b11 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -1,4 +1,4 @@
-/*global setTimeout: false, console: false */
+/*global setImmediate: false, setTimeout: false, console: false */
(function () {
var async = {};
@@ -74,9 +74,16 @@
//// nextTick implementation with browser-compatible fallback ////
if (typeof process === 'undefined' || !(process.nextTick)) {
- async.nextTick = function (fn) {
- setTimeout(fn, 0);
- };
+ if (typeof setImmediate === 'function') {
+ async.nextTick = function (fn) {
+ setImmediate(fn);
+ };
+ }
+ else {
+ async.nextTick = function (fn) {
+ setTimeout(fn, 0);
+ };
+ }
}
else {
async.nextTick = process.nextTick;