在Node.js的回调要么始终异步或同步始终?或者他们是否是"有时候一个人,有时候对方"? [英] Are callbacks in node.js either always asynchronous or always synchronous? Or can they be "sometimes one, sometimes the other"?

查看:119
本文介绍了在Node.js的回调要么始终异步或同步始终?或者他们是否是"有时候一个人,有时候对方"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和使得node.js中玩弄的东西,我(和其他人一样谁是有史以来开始学习节点)有一个关于它的异步性的问题。我四处搜寻了一下,但关于​​它的回答(也许我只是没有搜索非常好...)找不到这个具体问题,所以这里有云:

I'm toying with making something in node.js, and I (like everyone else who's ever started learning node) have a question about the asynchronous nature of it. I searched around a bit, but couldn't find this specific question about it answered (maybe I just didn't search very well...), so here goes:

的node.js的回调,总体来说的保证的如果文件是这么说的不同步?如果你把你自己的函数,采取回调,你应该以这样的方式设计,使他们要么总是异步或同步始终?或者他们是否是同步的,有时,有时不?

Are node.js callbacks, in general, guaranteed to be asynchronous if the documentation says so? If you make your own functions that take callbacks, should you design in such a way so that they're either always asynchronous or always synchronous? Or can they be sometimes synchronous, sometimes not?

作为一个例子,假设你想在互联网上加载一些数据,并创建了一个函数来加载它。而是让你决定缓存以备将来调用数据不经常改变。你能想象有人会写一个函数是这样的:

As an example, lets say you wanted to load some data over the internet, and you created a function to load it. But the data doesn't change very often, so you decide to cache it for future calls. You could imagine that someone would write that function something like this:

function getData(callback) {
    if(dataIsCached) {
        callback(cachedData)
    } else {
        fetchDataFromNetwork(function (fetchedData) {
            dataIsCached = true;
            cachedData = fetchedData;
            callback(fetchedData);
        });
    }
}

在哪里fetchDataFromNetwork是异步执行其回调的功能(这是伪略codeY,但我希望你明白我的意思)。

Where fetchDataFromNetwork is a function that executes its callbacks asynchronously (this is slightly pseudo-codey, but I hope you understand what I mean).

此功能仅异步触发如果数据没有被缓存,如果是高速缓存,它只是直接执行回调。在这种情况下,函数的异步性质,毕竟,完全没有必要。

This function will only fire asynchronously if the data isn't cached, if it is cached it just executes the callback directly. In that case, the asynchronous nature of the function is, after all, totally unnecessary.

时这种事情望而却步?如果函数的第二行是的setTimeout(函数(){回调(cachedData)}),0)来代替,以保证它异步触发?

Is this kind of thing discouraged? Should the second line of the function be setTimeout(function () {callback(cachedData)}), 0) instead, to guarantee that it fires asynchronously?

我问的原因是,我看到了一些codeA前阵子在哪里回调里面的code简单地认为回调外的函数的其他人都在里面了code之前执行回电话。我退缩了一点那个,想但你怎么知道的回调将异步火呢?如果它不需要和同步执行?为什么你曾经认为每一个回调是有保障是异步的?

The reason I'm asking is that I saw some code a while back where the code inside the callback simply assumed that the rest of the function outside the callback had executed before the code inside the callback. I recoiled a little at that, thinking "but how do you know that the callback will fire asynchronously? what if it doesn't need to and executes synchronously? why would you ever assume that every callback is guaranteed to be asynchronous?"

在这一点上的任何澄清会大大AP preciated。谢谢!

Any clarification on this point would be much appreciated. Thanks!

推荐答案

您的假设是正确的。

是node.js的回调,一般而言,保证是异步的,如果文件是这么说的?

Are node.js callbacks, in general, guaranteed to be asynchronous if the documentation says so?

是的。当然,也有与异步回调和功能与同步回调 功能,但没有一个一举两得。

Yes. Of course, there are functions with async callbacks and functions with sync callbacks, but none which do both.

如果你让自己的函数,采取回调,你应该以这样的方式设计,使他们要么总是异步或同步总是

If you make your own functions that take callbacks, should you design in such a way so that they're either always asynchronous or always synchronous?

它们可以是有时同步,有时没有诸如缓存?难道这种事情气馁?

They could be sometimes synchronous, sometimes not, such as a cache? Is this kind of thing discouraged?

是的。 非常喜欢d̲̭i̫̰̤̠͎͝ͅͅs͙̙̠c̙͖̗̜o͇̮̗̘͈̫ų̗͔̯ŕa҉̗͉͚͈͈̜g͕̳̱̗e҉̟̟̪͖̠̞ͅd͙͈͉̤̞̞̩.

如果函数的第二行是的setTimeout(函数(){回调(cachedData)}),0)来代替,以保证它异步触发?

Should the second line of the function be setTimeout(function () {callback(cachedData)}), 0) instead, to guarantee that it fires asynchronously?

是的,这是个好主意,但在节点你宁愿使用 setImmediate process.nextTick 代替的setTimeout 。结果
或使用的承诺,这也保证不同步,这样你就不必关心自己的事情推迟

Yes, that's a good idea, though in node you'd rather use setImmediate or process.nextTick instead of setTimeout.
Or use promises, which do guarantee asynchrony, so that you don't have to care about delaying things yourself.

我看到一些code而回,其中回调里面的code简单地认为回调外的函数的其余部分已经回调里面的code之前执行。我退缩了一点那个

I saw some code a while back where the code inside the callback simply assumed that the rest of the function outside the callback had executed before the code inside the callback. I recoiled a little at that

是啊,可以理解的。即使API你使用异步保证,它仍然会比较好写code,以便它可以在将要执行的顺序读取。如果可能的话,你应该把那些异步回调之前立即执行(例外证明这一规则)。

Yeah, understandable. Even if the API you're using guarantees asynchrony, it still would be better to write code so that it can be read in the order it would be executed. If possible, you should place the things that are executed immediately before the async callback (the exception proves the rule).

这篇关于在Node.js的回调要么始终异步或同步始终?或者他们是否是"有时候一个人,有时候对方"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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