c++ 等待异步回调
Why there is a multiple approach for handling asynchronous operations in Javascript? How do I choose the best fit for my use case? Lets deep dive to discuss on this topic. Even though myriads of post available on this topic. This story is intended to explain the concepts with simplified examples. If you are already aware of this concepts then its a refresher for you.
为什么在JavaScript中有多种方法来处理异步操作? 如何选择最适合自己的用例? 让我们深入讨论该主题。 即使有大量关于此主题的帖子。 这个故事旨在通过简化示例来解释这些概念。 如果您已经知道了这一概念,那么可以为您复习一下。
打回来 (Callback)
The callback is a function which is passed as a parameter to another function. The callback function is then executed perform some action. There are two types of callback function, they are:
回调是一个作为参数传递给另一个函数的函数。 然后执行回调函数,执行一些操作。 回调函数有两种类型,它们是:
Synchronous Callback Function
同步回叫功能
The synchronous callback functions are function which executes synchronously with out any blocking operation.
同步回调函数是在没有任何阻塞操作的情况下同步执行的函数。
In the following example, the function sayHello
is invoked with text “Hello” and print
function acts as a callback function. From the sayHello
function, the text value is propagated to the callback function (print) and response is printed.
在以下示例中,函数sayHello
与文本“ Hello”一起调用,而print
函数充当回调函数。 从sayHello
函数,文本值将传播到回调函数(打印)并打印响应。
function print(data) {
console.log(data)
}function sayHello(text, callback) {
callback(text)
}sayHello("Hello", print)
2. Asynchronous Callback
2.异步回调
The asynchronous callback functions are functions passed as an parameter for asynchronous operation and gets invoked once the operation completed.