async await

async/awaitpromise

1)理解異步

  簡化Promise對象的使用:不在使用then()來指定回調函數。async

      若是失敗用async/await怎麼使用:try...catch     catch獲得的就是promise裏失敗的    不用try..catch  則await拿到的就是then函數

  同步編碼方式方式實現異步流程編碼

2)使用對象

  哪裏使用await?  寫在promise左側  也就是寫在一個返回promise對象的表達式的左側:左側獲得的再也不是Promise,而是promise異步成功的值回調函數

  哪裏使用async?  await 所在最近函數定義的左側同步

async test(){it

  return 1test

  //throw new Error()   //promise返回一個失敗的狀態

}

const promise =  await test()  //promise=1

await獲得的值爲async函數返回的值

相關文章
相關標籤/搜索