promise 和 setTimeout 在任务队列的执行顺序

setTimeout(() => {
    console.log(1)
});
const a = new Promise((resolve,reject)=>{
    console.log(2);
    resolve(3)
})
setTimeout(() => {
    console.log(4)
});
const b = new Promise((resolve,reject)=>{
    console.log(5)
    resolve(6)
})
b.then(value=>console.log(value))
a.then(value=>console.log(value))

结果

2
5
6
3
1
4

总结:

1、promise和process.nextTick在程序中是先于settimtout执行的;

2、在promise和process.nextTick里面的console语句的执行速度是三个中最快,它与promise和process.nextTick执行的顺序无关,只与promise和process.nextTick声明的顺序有关;

原文:https://www.jianshu.com/p/40e59da2b6ae

posted @ 2019-12-05 19:51  刘金宇  阅读(311)  评论(0编辑  收藏  举报