How to return the Promise.all fetch api json data?

前端 未结 5 2176
滥情空心
滥情空心 2021-01-05 15:45

How to I consume the Promise.all fetch api json data? It works fine to pull it if I don\'t use Promise.all. With .all it actually returns the values of the query in the cons

5条回答
  •  感情败类
    2021-01-05 15:53

    Using Promise.all to fetch the json responses of each of the API's-

    CODE:

    Promise.all([
      API_1_Promise,
      API_2_Promise,
      API_3_Promise])
      .then(allResults => console.log(allResults))
      .catch(err => console.log(err))
    

    In which API_1_Promise,API_2_Promise,API_3_Promise is defined as

    API_1_Promise = fetch(`API_URL_1`, {  *Add required headers* }).then(response => response.json())
    
    API_2_Promise = fetch(`API_URL_2`, {  *Add required headers* }).then(response => response.json())
    
    API_3_Promise = fetch(`API_URL_3`, { *Add required headers* }).then(response => response.json())
    

    RESPONSE: This will print the array of responses from all the API calls In console-->

    [JSON_RESPONSE_API1, JSON_RESPONSE_API2, JSON_RESPONSE_API3]
    

提交回复
热议问题