为什么回调函数允许我们用Java异步执行操作?

为什么回调函数允许我们用Java异步执行操作?

问题描述:

我读过回调使JavaScript异步进行.但是我不确定我是否理解解释.这就是我得到的

I've read that callbacks make JavaScript asynchronously. But I'm not sure if I understood the explanation. This is what I get

回调函数使我们能够异步执行操作,因为它们可以确保 回调之前的行在加载之前已完全完成 下一行.

Callback functions allow us to do things asynchronously, since they ensure that the lines prior to the callback are completely finished before loading the next line.

是真的吗?谢谢

首先,不是回调启用了任何功能. node.js甚至基于浏览器的javascript中的给定操作要么是异步的,要么不是异步的.它实际上与回调无关,尽管回调通常用于传达异步操作的结果.

First off, it isn't the callback that enables anything. A given operation in node.js or even browser-based javascript either is or isn't asynchronous. It really has nothing to do with the callback, though a callback is typically used to communicate results of an asynchronous operation.

例如,Javascript的array.forEach()使用回调,但是它不是异步的.因此,异步操作是异步的,因为它们的基础实现是非阻塞的.您可以通过调用函数来开始操作,该操作在后台进行,其余代码继续运行.同时,异步操作完成后,通常通常需要告诉您代码已完成,并可能传达一些结果.选择了回调函数来传达异步操作的完成情况.

For example, Javascript's array.forEach() uses a callback, but it is not asynchronous. So, async operations are async because their underlying implementation is non-blocking. You start the operation by making a function call, the operation proceeds in the background and the rest of your code continues to run. Meanwhile, when the asynchronous operation completes, it usually then needs to tell your code that it is done and perhaps communicate some results. A callback function is the chosen mechanism for communicating the completion of the async operation.

我读过回调使JavaScript异步进行.

I've read that callbacks make JavaScript asynchronously.

不,那不是真的.回调也可以与同步操作一起使用.仅仅因为使用回调不会使任何事情异步.操作的基础本机代码实现必须是异步的(例如Ajax调用或其他网络操作).回调用于传达异步操作的结果.它们也有许多其他非异步用途.因此,回调只是异步操作中使用的一种工具,而回调也是具有许多其他用途的工具.你不能说callback === asynchronous.

No, that is not really true. Callbacks can be used with synchronous operations too. Just because one uses a callback does not make anything asynchronous. The underlying native code implementation of an operation must be asynchronous (such as an Ajax call or other networking operation). Callbacks are used to communicate the results of asynchronous operations. They are also have many other non-asynchronous uses too. So, a callback is just one tool used in an asynchronous operation and a callback is a tool with many other uses too. You cannot say callback === asynchronous.

回调函数使我们能够异步执行操作,因为它们 确保回调之前的行已完全完成 在加载下一行之前.

Callback functions allow us to do things asynchronously, since they ensure that the lines prior to the callback are completely finished before loading the next line.

很难确切地说出您的意思,但这对我来说是错误的.使用异步操作时,通常不会按照文件中列出的顺序执行代码.例如,如果您这样做:

It is hard to tell exactly what you mean by this, but it sounds wrong to me. When using asynchronous operations, code is typically not executed in the order laid out in the file. For example, if you did this:

console.log("async start");
callSomeAsyncOperation(function(result) {
    console.log("async done");
});
console.log("I'm here now");

您会在日志中看到它:

async start
I'm here now
async done


解释了回调和事件队列

了解异步操作的工作方式也可能很有用. Javascript可处理事件队列.给定的Javascript代码序列运行完毕.完成后,引擎将在事件队列中查找是否还有其他事件要处理.如果是这样,则会拉出队列中的第一个事件,并调用为该事件注册的回调.这将开始运行新的Javascript代码序列.该代码将继续运行,直到完成为止.完成后,引擎将检查另一个事件.如果存在,则通过调用与该事件关联的回调对其进行处理.当没有更多事件要处理时,引擎将进入睡眠状态以等待下一个事件.当事件发生时(在主要Javascript thead之外),然后将其添加到队列中,并将其添加到队列中的过程会导致JS引擎唤醒并为该事件提供服务.

It may also be useful to understand how an asynchronous operation works. Javascript works off an event queue. A given sequence of Javascript code runs to its completion. When that completes, the engine looks in the event queue to see if there are any more events to process. If so, the first event in the queue is pulled out and a callback that was registered for that event is called. This starts a new sequence of Javascript code running. That code continues to run until it finishes. When it finishes, the engine checks for another event. If there is one, it is then processed by calling a callback associated with that event. When there are no more events to process, the engine goes to sleep waiting for the next event. When an event occurs (outside the main Javascript thead), it is then added to the queue and that process of adding it to the queue causes the JS engine to wake up and service that event.

在编写Javascript时,您通常会为事件注册和事件处理程序.这在Javascript中的工作方式是,您说出您感兴趣的事件(可能还包括指定其他信息,例如您要寻找事件的对象),然后将其传递给函数引用.本质上,您是在告诉Javascript引擎您希望它在发生此事件时调用函数引用.这种类型的功能引用称为回调".它只是一个普通的函数,但是在其中使用它的上下文称为回调",因为将来某些时候某些其他代码将通过执行函数来回调您".然后,您可以将适当的代码放在可以响应该事件的函数引用中(在该回调函数内).根据事件的类型,您可能只会被呼叫一次,或者每次事件发生时都会呼叫您.

When writing your Javascript, you will often register and event handler for an event. The way this works in Javascript is that you say what event you're interested in (which may also include specifying some other information like what object you're looking for events on) and then you pass it is a function reference. You are, in essence, telling the Javascript engine that you want it to call your function reference when this event occurs. This type of function reference is referred to as a "callback". It's just a normal function, but the context in which it is being used is called a "callback" because some other code will "call you back" at some time in the future by executing your function. You can then place appropriate code in that function reference (inside that callback function) that can respond to that event. Depending upon the type of event, you may only get called once or it may call you every time that event occurs.

您可以在以下参考资料中了解有关此事件队列和回调如何工作的更多信息:

You can read more about how this event queue and callbacks work in these references:

在等待回调时运行任意代码在Node中?

非阻塞http服务器中的阻塞代码

Javascript/Node中隐藏的线程永远不会执行用户代码:是否可能,如果这样,是否会导致出现竞争状态的不可思议的可能性?

JavaScript如何处理背景?(写过关于浏览器的内容,但是概念是相同的)

How does JavaScript handle AJAX responses in the background? (written about the browser, but concept is the same)