什么事件指定在tick.js中打勾时结束?

我已经读过,tick是一个执行单元,其中nodejs事件循环决定在其队列中运行所有内容,但除了明确说出process.nextTick()什么事件导致node.js事件循环开始处理新的tick ? 它在I / O上等待吗? 怎么样的CPU约束计算? 还是每当我们进入一个新的function?

process.nextTick()不会导致Node.JS开始新的打勾。 它会使提供的代码等待下一个打勾。

这是理解它的一个很好的资源: http : //howtonode.org/understanding-process-next-tick

至于得到滴答的事件,我不相信运行时提供的。 你可以“伪造”它:

 var tickEmitter = new events.EventEmitter(); function emit() { tickEmitter.emit('tick'); process.nextTick( emit ); } tickEmitter.on('tick', function() { console.log('Ticked'); }); emit(); 

编辑:为了回答你的其他问题,另一篇文章做了一个非常杰出的工作来certificate: 一个Node.js事件循环到底是什么?

nextTick在当前正在执行的Javascript将控制权返回给事件循环(例如,完成执行)时注册要调用的callbacknextTick 。 对于一个CPU限制的操作,这将在函数完成时执行。 对于一个asynchronous操作,这将是asynchronous操作开始时,任何其他即时代码完成(而不是当asynchronous操作本身已经完成,因为它将完成从事件队列服务完成后将进入事件队列) 。

来自process.nextTick()的node.js文档 :

一旦当前的事件循环运行完成,调用callback函数。

这不是setTimeout(fn,0)的简单别名,它更有效率。 它在任何其他I / O事件(包括定时器)在事件循环的随后滴答中触发之前运行。

一些例子:

 console.log("A"); process.nextTick(function() { // this will be called when this thread of execution is done // before timers or I/O events that are also in the event queue console.log("B"); }); setTimeout(function() { // this will be called after the current thread of execution // after any `.nextTick()` handlers in the queue // and after the minimum time set for setTimeout() console.log("C"); }, 0); fs.stat("myfile.txt", function(err, data) { // this will be called after the current thread of execution // after any `.nextTick()` handlers in the queue // and when the file I/O operation is done console.log("D"); }); console.log("E"); 

输出:

 A E B C D