You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Async/Await:Python与JavaScript执行差异及原因咨询

Async/Await在Python与JavaScript中的执行差异疑问

我正在学习异步编程中的Async/Await,原本以为await关键字会等待Promise解析完成后再继续执行后续代码。基于这个理解,我分别编写了Python和JavaScript的测试脚本,但运行后两者的输出存在明显差异:

Python脚本输出

main() start
hello() start
hello() stop
main() stop

JavaScript脚本输出

main() start
hello() start
hello() stop

我用PowerShell命令Measure-Command { node .\async_await.js | out-default }测量JavaScript脚本的耗时,结果显示总耗时约5.12秒,而非预期的8秒。

我想确认:

  • JavaScript是否会等待hello()但不会等待main方法中的stall()?
  • 如果是,为什么JavaScript不等待stall()执行完成?
  • 如果不是,JavaScript在await这两个函数时的执行逻辑是什么?

内容的提问来源于stack exchange,提问作者asdf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 14:50:29