setTimeout(()=>{
console.log('a');
},0);
var p=new Promise(resolve=>{
console.log('b');
setTimeout(()=>{console.log('f')},0);
resolve();
})
p.then(()=>{console.log('c')});
p.then(()=>{console.log('d')});
console.log('e');
提问 程序的输出顺序
//输出 b e c d a f
大家都知道 js中异步程序 会放在 一个事件队列中 等待 主程序中执行完成后 才执行
那 promise的中的 then方法可能有很纳闷呢 它属于什么?
其实很这个就是微任务 但是它的执行顺序 在异步函数执行前面
不管是微任务还是异步函数 和主程序 都是先进先出 谁在前面 先执行谁
注意 : promise并不是 所有的都是异步执行 只是里面包括着异步执行 然后对里面的异步函数 进行处理
console.log('b') 还是照样在 主程序中的
按照我们上面的分析 最后的 执行顺序就是
b e c d a f