Promise

 
  • Promiseasync-await转化

      // async格式
      async function async1() {
        console.log(111)
        await async2()
        console.log(222)
      }
      async function async2() {
        console.log(333)
      }
      // 转Promise格式
      function asyncs() {
        console.log(111)
        new Promise(resolve=> {
          console.log(333)
          resolve()
        }).then(()=>{
          console.log(222)
        })
      }
    
  • await写在async里边的原因

    虽然await阻塞了,但await在async中,async不会阻塞,它内部所有的阻塞都被封装在一个promise对象中异步执行。 任何一个await语句后面的 Promise 对象变为reject状态,那么整个async函数都会中断执行,并且返回一个错误状态。 可以用try{...}catch(){...}来解决