Promise

37 阅读1分钟
Promise.resolve().then(() => {
    console.log(0);
    return Promise.resolve(4);
}).then((res) => {
    console.log(res);
});

Promise.resolve().then(() => {     
    console.log(1);   
}).then(() => {     
    console.log(2);   
}).then(() => {     
    console.log(3);   
}).then(() => {     
    console.log(5);   
}).then(() => {     
    console.log(6);   
})
0 1 2 3 4 5 6

原因:

  • 多个promise实例的then方法是交替执行的
  • 在then方法中返回一个promise实例,可能会有两个异步过程:pending状态变为fulfilled、将实例的then方法添加到微任务队列中