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

前端 未结 5 2189
滥情空心
滥情空心 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:58

    You can just throw await in front of your Promise instead of awaiting each individual fetch

    await Promise.all([
        fetch('https://jsonplaceholder.typicode.com/todos/1'),
        fetch('https://jsonplaceholder.typicode.com/todos/2')
      ]).then(async([aa, bb]) => {
        const a =  aa.json();
        const b =  bb.json();
        return [a, b]
      })
      .then((responseText) => {
        console.log(responseText);
    
      }).catch((err) => {
        console.log(err);
      });
    

    Hope this helps

提交回复
热议问题