Skip to content

Commit

Permalink
Replaced deprecated process.nextTick with setImmediate
Browse files Browse the repository at this point in the history
- Added test that causes Maximum call stack size exeeded error in node
when not using setImmediate
  • Loading branch information
taras committed Dec 6, 2014
1 parent e4b2f24 commit 6ab581c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/rsvp/asap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function asap(callback, arg) {
var browserWindow = (typeof window !== 'undefined') ? window : undefined
var browserGlobal = browserWindow || {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';

// test for web worker but not in IE10
var isWorker = typeof Uint8ClampedArray !== 'undefined' &&
Expand All @@ -23,8 +24,15 @@ var isWorker = typeof Uint8ClampedArray !== 'undefined' &&

// node
function useNextTick() {
var nextTick = process.nextTick;
// node version 0.10.x displays a deprecation warning when nextTick is used recursively
// setImmediate should be used instead instead
var version = process.versions.node.match(/^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)$/);
if (Array.isArray(version) && version[1] === '0' && version[2] === '10') {
nextTick = setImmediate;
}
return function() {
process.nextTick(flush);
nextTick(flush);
};
}

Expand Down Expand Up @@ -88,7 +96,7 @@ function attemptVertex() {

var scheduleFlush;
// Decide what async method to use to triggering processing of queued callbacks:
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
if (isNode) {
scheduleFlush = useNextTick();
} else if (BrowserMutationObserver) {
scheduleFlush = useMutationObserver();
Expand Down
25 changes: 25 additions & 0 deletions test/tests/extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2520,3 +2520,28 @@ describe("Thenables should not be able to run code during assimilation", functio
assert.strictEqual(thenCalled, false);
});
});

describe("on node 0.10.x, using process.nextTick recursively shows deprecation warning. setImmediate should be used instead.", function() {
// (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

specify("should not cause 'RangeError: Maximum call stack size exceeded'", function (done) {
var depth = 0;
var nextTick = function(promise) {
if (depth < 500) {
promise.then(function(){
process.nextTick(function(){
depth++;
nextTick(promise);
})
});
}
return promise;
};

nextTick(RSVP.resolve())
.then(function(){
done();
});
});

});

0 comments on commit 6ab581c

Please sign in to comment.