promise 练习

宏队列(dom事件,ajax,定时器)和微队列(promise);微队列先于宏队列执行。

setTimeout(() => {
  console.log(1);

}, 0);
new Promise(resolve => {
  console.log(2);
  resolve() //resolve()函数执行了才会触发then里面的回调函数,不执行resolve()then回调函数不会执行的

}).then(() => {
  console.log(3);

}).then(() => { console.log(4) })
console.log(5);
const first = () => new Promise((resolve, reject) => {
  console.log(3);
  const p = new Promise((resolve, reject) => {
    console.log(7);
    setTimeout(() => {
      console.log(5);
      resolve(6) //p的状态已经是fulfilled,不会再执行resolve()函数

    }, 0);
    resolve(1)
  })
  resolve(2)
  p.then(console.log) //then()是同步执行,把then()里面方法放入微队列中

})
first().then(console.log)
console.log(4);

相关推荐