asynchronous - 异步调用和回调有什么区别

标签 asynchronous callback

我对理解异步调用和回调之间的区别有点困惑。

我读了这篇文章 teach about CallBacks但没有一个答案地址它与异步调用有何不同 .

这是回调 = Lambda 表达式吗?

回调在不同的线程中运行?

谁能用通俗易懂的英语解释这一点?

最佳答案

很简单,回调不必是异步的。

http://docs.apigee.com/api-baas/asynchronous-vs-synchronous-calls

  1. Synchronous:

    If an API call is synchronous, it means that code execution will block (or wait) for the API call to return before continuing. This means that until a response is returned by the API, your application will not execute any further, which could be perceived by the user as latency or performance lag in your app. Making an API call synchronously can be beneficial, however, if there if code in your app that will only execute properly once the API response is received.

  2. Asynchronous:

    Asynchronous calls do not block (or wait) for the API call to return from the server. Execution continues on in your program, and when the call returns from the server, a "callback" function is executed.



在 Java、C 和 C# 中,“回调”通常是同步的(相对于“主事件循环”)。

另一方面,在 Javascript 中,回调通常是异步的——您传递一个将被调用的函数……但其他事件将继续处理,直到调用回调。

如果你不在乎什么 Javascript 事件以什么顺序发生 - 太好了。否则,在 Javascript 中管理异步行为的一种非常强大的机制是使用“promises”:

http://www.html5rocks.com/en/tutorials/es6/promises/

PS:
要回答您的其他问题:
  • 是的,回调可能是一个 lambda - 但这不是必需的。

    在 Javascript 中,几乎每个回调都是一个“匿名函数”(基本上是一个“lambda 表达式”)。
  • 是的,可以从不同的线程调用回调 - 但这当然不是必需的。
  • 回调也可能(并且经常会)产生一个线程(从而使自己“异步”)。

  • '希望有帮助

    ================================================== ==================

    你好,我们又见面了:

    Q: @paulsm4 can you please elaborate with an example how the callback and asynchronous call works in the execution flow? That will be greatly helpful


  • 首先,我们需要就“回调”的定义达成一致。这是一个很好的:

    https://en.wikipedia.org/wiki/Callback_%28computer_programming%29

    In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback.

  • 我们还必须定义“同步”和“异步”。基本上 - 如果回调在返回调用者之前完成所有工作,则它是“同步的”。如果它可以在被调用后立即返回给调用者——并且调用者和回调可以并行工作——那么它就是“异步的”。
  • 同步回调的问题是它们可能看起来“挂起”。异步回调的问题是你可能会失去对“排序”的控制——你不能保证“A”会在“B”之前出现。
  • 回调的常见示例包括:

    a) 按钮按下处理程序(每个不同的“按钮”将有不同的“响应”)。这些通常被“异步”调用(通过 GUI 的主事件循环)。

    b) 一个排序“比较”函数(所以一个常见的“sort()”函数可以处理不同的数据类型)。这些通常是“同步”调用的(由您的程序直接调用)。
  • 一个具体的例子:

    a) 我有一个带有“print()”函数的“C”语言程序。

    b) “print()”旨在使用三个回调之一:“PrintHP()”、“PrintCanon()”和“PrintPDF()”。

    c) "PrintPDF()"调用库以 PDF 格式呈现我的数据。它是同步的 - 在 .pdf 渲染完成之前,程序不会从“print()”返回。它通常进行得很快,所以没有问题。

    d) 我已经编写了“PrintHP()”和“PrintCanon()”来生成线程来对物理打印机进行 I/O。 “Print()”在线程创建后立即退出;实际的“打印”与程序执行并行进行。这两个回调是“异步的”。

  • 问:有道理吗?这有帮助吗?

    关于asynchronous - 异步调用和回调有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213948/

    相关文章:

    javascript - Angular 6 : async await some variable got it variable

    javascript - 在回调中调用 yield,redux saga 给了我一个异常为什么?

    java - JNI : Callback from JVM to C++ fails to run

    javascript - 在同步回调中​​使用 dust.js(异步)

    c# - 这是实现异步编程模型的好方法吗?

    javascript - 如何解决我的异步函数?

    python - 用另一个函数包装 tornado.gen.engine-wrapped 函数

    java - 动画完成时获取 animate().translation 回调

    javascript - 在 for 循环中修改数组后获取数组结果

    ruby-on-rails - 有没有办法在 Delayed::Job 中添加对失败尝试的回调?