c++ 等待异步回调_从回调到承诺到异步等待的旅程

c++ 等待异步回调

JavaScript is, strictly speaking, synchronous. It behaves asynchronously, though, thanks to its runtime environment — making it, in effect, asynchronous.

严格来说,JavaScript是同步的。 但是,由于运行时环境的原因,它的行为是异步的-实际上使它成为异步的。

These are many questions that you may not know the answer if you haven’t explored what asynchronous JavaScript is. In this article, I would speak of the following and dive deep into asynchronous programming with JavaScript.

如果您还没有探索异步JavaScript是什么,那么可能有很多问题您可能都不知道答案。 在本文中,我将谈到以下内容,并深入探讨使用JavaScript进行异步编程。

  1. The JavaScript event loop

    JavaScript事件循环
  2. Callbacks and callback hell

    回调和回调地狱
  3. Promises

    承诺
  4. Asynchronous chaining

    异步链接
  5. Async/Await

    异步/等待

1. JavaScript事件循环(1. JavaScript Event Loop)

With the introduction of ES6, asynchronous programming became very popular in the JavaScript community. Up until then, JavaScript did not have a direct notion of asynchrony introduced. The JavaScript engine does not run in isolation. It runs in a hosting environment. There is a built-in mechanism available in JavaScript runtime called the event loop. It handles the execution of multiple chunks of your program over time, each time invoking the JS Engine.

随着ES6的引入,异步编程在JavaScript社区中变得非常流行。 直到那时,JavaScript还没有引入异步的直接概念。 JavaScript引擎并非孤立运行。 它在托管环境中运行。 JavaScript运行时中有一个内置的机制称为事件循环。 每次调用JS引擎时,它都会处理程序中多个块的执行。

Image for post

When an asynchronous function (e.g., a network request) with a callback is processed, the callstack executes the asynchronous function, and the callback gets added to the callback queue. When the network request completes, the JavaScript runtime removes the asynchronous function from the callstack. Then the event loop picks the next function in the callback queue (this could be the callback function that was last added or a function that was added to the queue earlier) and pushes it to the callstack. Since the callback function was added to the callback queue, it will certainly end up in the callstack sometime in the future. Due to this, the thread is not blocked until a computationally intensive task is complete.

当处理带有回调的异步功能(例如,网络请求)时,调用堆栈将执行异步功能,并将回调添加到回调队列中。 网络请求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值