Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 774 Bytes

File metadata and controls

29 lines (22 loc) · 774 Bytes

Promise

  • axios 回來的資料會是Promise,必須用then去接
  • 或者是使用await等待axios完成,才執行console.log
  getB() {
    const APromise = axios.get('XXXX') // Promise {<pending>}
      .then((res)=>{
        console.log(res) // {data: Array(2), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
      })
      .catch()
  async getA() {
    const BPromise = await axios.get('XXXX') 
    console.log(queryRolesPromise)  // // {data: Array(2), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
  • 一次解析多的Promise
    const APromise = axios.get(API.a);
    const BPromise = axios.get(API.b);
    const [AResponse, BResponse] = await Promise.all([APromise, BPromise]);