diff --git a/doc/api.txt b/doc/api.txt index e59c484a77..e6a54c24d5 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -123,6 +123,9 @@ Returns the memory usage of the Node process. It looks like this + +heapTotal+ and +heapUsed+ refer to V8's memory usage. ++process.nextTick(callback)+:: +On the next loop around the event loop call this callback. + +process.exit(code=0)+:: Ends the process with the specified code. By default it exits with the success code 0. diff --git a/lib/multipart.js b/lib/multipart.js index 99be6e1f1b..a5afeff3ba 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -7,9 +7,9 @@ exports.parse = function(options) { try { var stream = new exports.Stream(options); } catch (e) { - setTimeout(function() { + process.nextTick(function() { promise.emitError(e); - }, 0); + }); return promise; } diff --git a/src/node.js b/src/node.js index e6bab2e5dc..798849d85f 100644 --- a/src/node.js +++ b/src/node.js @@ -377,6 +377,30 @@ var eventsModule = createInternalModule('events', function (exports) { var events = eventsModule.exports; +// nextTick() + +var nextTickQueue = []; +var nextTickWatcher = new process.IdleWatcher(); +nextTickWatcher.setPriority(process.EVMAXPRI); // max priority + +nextTickWatcher.callback = function () { + var l = nextTickQueue.length; + while (l--) { + var cb = nextTickQueue.shift(); + cb(); + } + if (nextTickQueue.length == 0) nextTickWatcher.stop(); +}; + +process.nextTick = function (callback) { + nextTickQueue.push(callback); + nextTickWatcher.start(); +}; + + + + + // Signal Handlers function isSignal (event) { @@ -829,9 +853,9 @@ function loadModule (request, parent) { debug("found " + JSON.stringify(id) + " in cache"); // In cache var module = moduleCache[id]; - setTimeout(function () { + process.nextTick(function () { loadPromise.emitSuccess(module.exports); - }, 0); + }); } else { debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths)); // Not in cache @@ -868,11 +892,11 @@ Module.prototype.loadObject = function (filename, loadPromise) { var self = this; // XXX Not yet supporting loading from HTTP. would need to download the // file, store it to tmp then run dlopen on it. - setTimeout(function () { + process.nextTick(function () { self.loaded = true; process.dlopen(filename, self.exports); // FIXME synchronus loadPromise.emitSuccess(self.exports); - }, 0); + }); }; function cat (id, loadPromise) { diff --git a/test/mjsunit/test-next-tick.js b/test/mjsunit/test-next-tick.js new file mode 100644 index 0000000000..1dd20574ad --- /dev/null +++ b/test/mjsunit/test-next-tick.js @@ -0,0 +1,28 @@ +process.mixin(require("./common")); + +var complete = 0; + +process.nextTick(function () { + complete++; + process.nextTick(function () { + complete++; + process.nextTick(function () { + complete++; + }); + }); +}); + +setTimeout(function () { + process.nextTick(function () { + complete++; + }); +}, 50); + +process.nextTick(function () { + complete++; +}); + + +process.addListener('exit', function () { + assert.equal(5, complete); +});