setImmediate 与 nextTick [英] setImmediate vs. nextTick

查看:23
本文介绍了setImmediate 与 nextTick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js 0.10 版今天发布并引入了 setImmediate.API 更改 文档建议在进行递归 nextTick 调用时使用它.

Node.js version 0.10 was released today and introduced setImmediate. The API changes documentation suggests using it when doing recursive nextTick calls.

MDN 所说来看,它看起来非常相似process.nextTick.

From what MDN says it seems very similar to process.nextTick.

什么时候应该使用nextTick,什么时候应该使用setImmediate?

When should I use nextTick and when should I use setImmediate?

推荐答案

如果您想将函数排在事件队列中已经存在的任何 I/O 事件回调之后,请使用 setImmediate.使用 process.nextTick 将函数有效地排在事件队列的头部,使其在当前函数完成后立即执行.

Use setImmediate if you want to queue the function behind whatever I/O event callbacks that are already in the event queue. Use process.nextTick to effectively queue the function at the head of the event queue so that it executes immediately after the current function completes.

因此,如果您尝试使用递归分解长时间运行的 CPU 密集型作业,您现在需要使用 setImmediate 而不是 process.nextTick 将下一次迭代排队,否则任何 I/O 事件回调都不会有机会在迭代之间运行.

So in a case where you're trying to break up a long running, CPU-bound job using recursion, you would now want to use setImmediate rather than process.nextTick to queue the next iteration as otherwise any I/O event callbacks wouldn't get the chance to run between iterations.

这篇关于setImmediate 与 nextTick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆