Axios: chaining multiple API requests

前端 未结 7 806
失恋的感觉
失恋的感觉 2020-12-07 14:15

I need to chain a few API requests from the Google Maps API, and I\'m trying to do it with Axios.

Here is the first request, which is in componentWillMount()

7条回答
  •  不知归路
    2020-12-07 14:42

    Have you used axios.all ? You can try with something similar:

    axios.all([axios.get(`firstrequest`),
               axios.get(`secondrequest`),
               axios.get(`thirdrequest`)])
         .then(axios.spread((firstResponse, secondResponse, thirdResponse) => {  
             console.log(firstResponse.data,secondResponse.data, thirdResponse.data);
         }))
         .catch(error => console.log(error));
    

    This will take all your get and will put it inside a response that has to be called with .data like: firstResponse.data

提交回复
热议问题