首页 > 其他分享 >多层嵌套 promise then 执行顺序

多层嵌套 promise then 执行顺序

时间:2022-10-18 18:23:10浏览次数:88  
标签:resolve console E5% 多层 嵌套 任务 promise 执行

在看 js 事件循环的时候,看到一个有趣的 promise then 执行顺序的题,想了好久,终于想明白了,这里记录一下。

 

大家先想下这里的执行顺序是什么。

new Promise(resolve => {                  // 1
  setTimeout(()=>{                        // 2
      console.log(666);                   // 3
      new Promise(resolve => {            // 4
        resolve();                        // 5      
      })                                  // 6       
      .then(() => {console.log(777);})    // 7
  })                                      // 8       
  resolve();                              // 9
 })                                       // 10
 .then(() => {                            // 11
         new Promise(resolve => {         // 12
           resolve();                     // 13
         })                               // 14
         .then(() => {console.log(111);}) // 15
         .then(() => {console.log(222);});// 16
 })                                       // 17
 .then(() => {                            // 18
         new Promise((resolve) => {       // 19
           resolve()                      // 20
         })                               // 21
        .then(() => {                     // 22
             new Promise((resolve) => {   // 23
               resolve()                  // 24
             })                           // 25
            .then(() => {console.log(444)})// 26
         })                                // 27
        .then(() => {                      // 28
           console.log(555);               // 29
        })                                 // 30
})                                         // 31
.then(() => {                              // 32
  console.log(333);                        // 33
})                                         // 34

 

 

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

1,2,3,4,5,6,7

这里最疑惑的我想应该就是 333 为什么先比 444 和 555 执行了,这里首先要明确一点  promise then函数回调执行完毕后才会开始下一个 then 函数,当多个 then 链式调用的时候,如果一个 then 函数放入微任务队列,没有执行完,则之后的 then 都会先忽略,继续向下寻找同步任务继续执行。

首先,大家都知道 setTimeout 是宏任务,则一定在下一轮事件循环的时候才执行,则他的执行优先级最低,promise是同步任务,会先执行,promise.then() 是微任务,当遇到微任务的时候,会先放入微任务队列,继续向下寻找同步任务,同步任务执行完之后,开始执行微任务。

执行到 22 行逻辑:

这里当执行到 22 行时, then的参数整体作为一个函数放入微任务队列中,因为这里还没执行,所以 555 的 then 就暂时不执行,继续下一个 333 的 then 放入微任务队列,接下来没有同步任务了,开始执行微任务队列,当执行到 23 行的时候,这个微任务先执行 promise 同步函数,将then 444 又放入了 微任务队列,接下来没有课执行的同步任务了,则开始执行微任务队列 444 打印输出,至此, 22-27 行终于执行结束了,开始进入 下一个 then 微任务,并放入微任务队列,打印 555。

22-34 行执行结束:

进入下一个宏任务,执行第 3 行,同步任务,打印 666,执行 4-5 行,同步任务,进入 then,执行 7 行,微任务。

 

转载请注明来源:https://www.cnblogs.com/beileixinqing/p/16803587.html

参考文章:https://347830076.github.io/myBlog/javascript/%E6%90%9E%E6%B8%85%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF%E5%AE%8F%E4%BB%BB%E5%8A%A1%E5%BE%AE%E4%BB%BB%E5%8A%A1.html#%E6%89%A9%E5%B1%95%E5%BB%B6%E4%BC%B8

标签:resolve,console,E5%,多层,嵌套,任务,promise,执行
From: https://www.cnblogs.com/beileixinqing/p/16803587.html

相关文章

  • 手写promise
    分析对于promise的复现,我们得先考虑promise的使用。使用时可以进行new操作,那么可以用构造函数获取class来构建存在then方法可以调用resolve和reject方法有三种状态p......
  • axios : Promise based HTTP client for the browser and node.js
    axiosPromisebasedHTTPclientforthebrowserandnode.jsFeaturesMake​​XMLHttpRequests​​fromthebrowserMake​​http​​requestsfromnode.jsSupportsthe......
  • Promise 捕获程序中的失败
    代码varA=newPromise(function(resolve,reject){console.log('dosth.');setTimeout(()=>{console.log('~~~~~');resolve('ok');//reject......
  • Java8Stream的flatmap应用(区别map)-优化嵌套for循环条件筛选
    场景Java8新特性-Stream对集合进行操作的常用API:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126070657前面讲Stream的常用api中讲到map的使用......
  • Promise 只处理成功回调
    只处理成功varA=newPromise(function(resolve,reject){console.log('dosth.');setTimeout(()=>{console.log('~~~~~');resolve('ok');rej......
  • 用嵌套if语句显示一个数的约数
    //divisors.c--使用嵌套if语句显示一个数的约数#include<stdio.h>#pragmawarning(disable :4996)#include<stdbool.h>intmain(){  unsignedlongnum;  ......
  • Python(for和while)循环嵌套及用法
    Python 不仅支持if语句相互嵌套,while和for循环结构也支持嵌套。所谓嵌套(Nest),就是一条语句里面还有另一条语句,例如for里面还有for,while里面还有while,甚至while......
  • Python if语句嵌套(入门必读)
    前面章节中,详细介绍了3种形式的条件语句,即if、ifelse和ifelifelse,这3种条件语句之间可以相互嵌套。例如,在最简单的if语句中嵌套ifelse语句,形式如下:if表达......
  • js 外部调用 嵌套函数
     在函外部直接调用函数的内部函数是不可以的,因为是向外查找的 所以不能直接内部函数  functionone(){functiontwo(){varb=30;......
  • 嵌套循环的使用
    使用嵌套循环,输出如下字符:$$$$$$$$$$$$$$$----------------------------#include<iostream>#include<string.h> #include<limits.h>#include<float.h>#include<stdio.h......