From 738638da40811f560611600b3dac72316785225e Mon Sep 17 00:00:00 2001 From: Morgan Roderick Date: Fri, 27 Apr 2018 11:30:08 +0100 Subject: [PATCH] Extract nextTick to own file --- lib/sinon/behavior.js | 15 +-------------- lib/sinon/util/core/next-tick.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 lib/sinon/util/core/next-tick.js diff --git a/lib/sinon/behavior.js b/lib/sinon/behavior.js index 03faba8a6..bed78b6b8 100644 --- a/lib/sinon/behavior.js +++ b/lib/sinon/behavior.js @@ -2,6 +2,7 @@ var extend = require("./util/core/extend"); var functionName = require("./util/core/function-name"); +var nextTick = require("./util/core/next-tick"); var valueToString = require("./util/core/value-to-string"); var slice = Array.prototype.slice; @@ -9,20 +10,6 @@ var join = Array.prototype.join; var useLeftMostCallback = -1; var useRightMostCallback = -2; -var nextTick = (function () { - if (typeof process === "object" && typeof process.nextTick === "function") { - return process.nextTick; - } - - if (typeof setImmediate === "function") { - return setImmediate; - } - - return function (callback) { - setTimeout(callback, 0); - }; -})(); - function getCallback(behavior, args) { var callArgAt = behavior.callArgAt; diff --git a/lib/sinon/util/core/next-tick.js b/lib/sinon/util/core/next-tick.js new file mode 100644 index 000000000..94aca873b --- /dev/null +++ b/lib/sinon/util/core/next-tick.js @@ -0,0 +1,20 @@ +"use strict"; + +var hasNextTick = typeof process === "object" && typeof process.nextTick === "function"; +var hasSetImmediate = typeof setImmediate === "function"; + +function nextTick(callback) { + setTimeout(callback, 0); +} + +if (hasNextTick) { + module.exports = process.nextTick; +} + +if (!hasNextTick && hasSetImmediate) { + module.exports = setImmediate; +} + +if (!hasNextTick && !hasSetImmediate) { + module.exports = nextTick; +}