简体   繁体   English

nodejs:什么时候应该使用`setImmediate(cb)`vs`cb()`?

[英]nodejs: When should I use `setImmediate(cb)` vs `cb()`?

When reading on The Callback Hell , I came across two different ways of calling a callback function in the exact same example linked above (last example in the linked section): 在阅读《回调地狱》时 ,在上面链接的相同示例(链接部分的最后一个示例)中,我遇到了两种不同的调用回调函数的方法:

cb();

and then 接着

setImmediate(cb);

for seemingly exactly the same thing. 看似完全一样的东西。 When do I use which of the two versions? 什么时候使用两个版本中的哪个版本?

With setImmediate you are posticipating the execution of the callback to the end of the event loop. 使用setImmediate您可以将回调的执行推迟到事件循环的结尾。 What does that mean? 这意味着什么? Nodejs will handle the other functions calls before your cb. Nodejs将 cb 之前处理其他函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM