js微任务有必要深究执行顺序吗。比如下面两段代码的执行顺序

一下两段代码单独执行或者同时执行的时候的打印顺序!

Promise.resolve()
    .then(() => {
        console.log(1);
        Promise.resolve()
            .then(() => {
                console.log(3);
                Promise.resolve()
                    .then(() => {
                        console.log(5);
                        Promise.resolve()
                            .then(() => {console.log(9)})
                            .then(() => console.log(10));
                    })
                    .then(() => console.log(6));
            })
            .then(() => console.log(4))
            .then(() => console.log(8));
    })
    .then(() => console.log(2))
    .then(() => console.log(7));




Promise.resolve()
    .then(() => {
        console.log(11);
        Promise.resolve()
            .then(() => {
                console.log(13);
                Promise.resolve()
                    .then(() => {
                        console.log(15);
                        Promise.resolve()
                            .then(() => {console.log(19)})
                            .then(() => console.log(110));
                    })
                    .then(() => console.log(16));
            })
            .then(() => console.log(14))
            .then(() => console.log(18));
    })
    .then(() => console.log(12))
    .then(() => console.log(17));

image.png

阅读 1.1k
1 个回答
Promise.resolve()
    .then(() => {
        console.log(1);
        Promise.resolve()
            .then(() => {
                console.log(2);
                Promise.resolve()
                    .then(() => {
                        console.log(4);
                        Promise.resolve()
                            .then(() => {console.log(7)})
                            .then(() => console.log(10));
                    })
                    .then(() => console.log(8));
            })
            .then(() => console.log(5))
            .then(() => console.log(9));
    })
    .then(() => console.log(3))
    .then(() => console.log(6));

打印的数字就是顺序,本质就是Promise执行顺序而已,没有加setTimeout什么的函数,不涉及宏任务微任务优先级问题。两个一起执行无非是先执行上面的再执行下面的而已,理解了Promise执行顺序就行。
这种题也就面试的时候会问到,背一下概念就行,其实没啥意义。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进