NodeJS:process.nextTick与即时回调 [英] NodeJS: process.nextTick vs Instant Callbacking

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

问题描述

我写了很多看起来像这样的模块:

I write lots of modules which look like this:

function get(index, callback) {
    if (cache[index] === null) {
        request(index, callback); // Queries database to get data.
    } else {
        callback(cache[index]);
    }
}

注意:这是的简化版我的实际代码。

该回调可以在同一执行中或在一段时间后调用。这意味着该模块的用户不确定首先运行哪个代码。

That callback is either called in the same execution or some time later. This means users of the module aren't sure which code is run first.

我的观察是,这样的模块重新引入了多线程的一些问题,该问题以前是由解决的。 JavaScript引擎。

My observation is that such module reintroduces some problems of the multi-threading which was previously solved by JavaScript engine.

问题:我应该使用 process.nextTick 还是确保在外部调用该回调是安全的

Question: should I use process.nextTick or ensure it's safe for the callback to be called outside the module?

推荐答案

这完全取决于您在回调函数中执行的操作。如果需要确保在 get 返回时尚未触发回调,则需要 process.nextTick 流;在许多情况下,您无需在意何时触发回调,因此无需延迟执行。不可能给出一个适用于所有情况的明确答案;始终将回调推迟到下一个刻度应该是安全的,但是这样做可能会效率较低,因此这是一个折衷。

It depends entirely on what you do in the callback function. If you need to be sure the callback hasn't fired yet when get returns, you will need the process.nextTick flow; in many cases you don't care when the callback fires, so you don't need to delay its execution. It is impossible to give a definitive answer that will apply in all situations; it should be safe to always defer the callback to the next tick, but it will probably be a bit less efficient that way, so it is a tradeoff.

唯一的情况我可以想到,如果真正需要在 之后调用 get ,但在调用回调之前。这可能是一种罕见的情况,也可能表明需要改进实际控制流程。您不应该完全依赖于何时确切地调用了回调,因此应该在调用 get 的那一点上设置它使用的任何环境。

The only situation I can think of where you will need to defer the callback for the next tick is if you actually need to set something up for it after the call to get but before the call to callback. This is perhaps a rare situation that also might indicate a need for improvement in the actual control flow; you should not be rely at all on when exactly your callback is called, so whatever environment it uses should already be set up at the point where get is called.

基于事件的控制流中存在某些情况(与基于回调的情况相对),您可能需要推迟实际的事件触发。例如:

There are situations in event-based control flow (as opposed to callback-based), where you might need to defer the actual event firing. For example:

function doSomething() {
    var emitter = new EventEmitter();
    cached = findCachedResultSomehow();
    if (cached) {
        process.nextTick(function() {
            emitter.emit('done', cached);
        });
    } else {
        asyncGetResult(function(result) {
            emitter.emit('done', result);
        });
    }
    return emitter;
}

在这种情况下,您需要推迟在缓存值的情况下发出,因为否则将在 doSomething 的调用者有机会附加侦听器之前发出事件。使用回调时,通常不需要考虑这一点。

In this case, you will need to defer the emit in the case of a cached value, because otherwise the event will be emitted before the caller of doSomething has had the chance to attach a listener. You don't generally have this consideration when using callbacks.

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

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