.$nexttick_了解process.nextTick()

.$nexttick

As you try to understand the Node.js event loop, one important part of it is process.nextTick().

当您尝试了解Node.js事件循环时 ,它的重要部分是process.nextTick()

Every time the event loop takes a full trip, we call it a tick.

每次事件循环进行一次完整的行程时,我们都将其称为对勾。

When we pass a function to process.nextTick(), we instruct the engine to invoke this function at the end of the current operation, before the next event loop tick starts:

当我们将一个函数传递给process.nextTick() ,我们指示引擎在下一个事件循环滴答开始之前,在当前操作结束时调用此函数:

process.nextTick(() => {
  //do something
})

The event loop is busy processing the current function code.

事件循环正在忙于处理当前功能代码。

When this operation ends, the JS engine runs all the functions passed to nextTick calls during that operation.

当该操作结束时,JS引擎将运行该操作期间传递给nextTick调用的所有函数。

It’s the way we can tell the JS engine to process a function asynchronously (after the current function), but as soon as possible, not queue it.

这是我们可以告诉JS引擎异步处理一个函数的方法(在当前函数之后),但是请尽快将其排队。

Calling setTimeout(() => {}, 0) will execute the function at the end of next tick, much later than when using nextTick() which prioritizes the call and executes it just before the beginning of the next tick.

调用setTimeout(() => {}, 0)将在下一个刻度结束时执行该功能,比使用nextTick()优先执行该调用并在下一个刻度开始之前执行时要晚得多。

Use nextTick() when you want to make sure that in the next event loop iteration that code is already executed.

当您要确保在下一个事件循环迭代中已执行代码时,请使用nextTick()

翻译自: https://flaviocopes.com/node-process-nexttick/

.$nexttick

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值