专栏名称: 码小辫
给程序员和编程爱好者分享计算机编程电子书以及相关的学习资源
今天看啥  ›  专栏  ›  码小辫

你不知道的 async、await 魔鬼细节

码小辫  · 公众号  ·  · 2024-07-23 17:10

文章预览

作者:Squirrel_ https://juejin.cn/post/7194744938276323384 0、前言 关于 promise、async/await 的使用相信很多小伙伴都比较熟悉了,但是提到 事件循环机制输出结果 类似的题目,你敢说都会? 试一试? 🌰1: async   function   async1  ( )  {      await   new   Promise ( ( resolve, reject ) =>  {         resolve()     })      console .log( 'A' ) } async1() new   Promise ( ( resolve ) =>  {      console .log( 'B' )     resolve() }).then( ()  =>  {      console .log( 'C' ) }).then( ()  =>  {      console .log( 'D' ) }) // 最终结果👉: B A C D 🌰2: async   function   async1  ( )  {      await  async2()      console .log( 'A' ) } async   function   async2  ( )  {      return   new   Promise ( ( resolve, reject ) =>  {         resolve()     }) } async1() new   Promise ( ( resolve ) =>  {      console .log( 'B' )     resolve() }).th ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照
总结与预览地址:访问总结与预览