我如何使用 foreach 或其他东西在本机反应中获取数据?

问题描述 投票:0回答:1

我有这个 API 网址

xxx.com/services/videos/xx/yy

xx = category param
yy = pagination param

我想要foreach这个类别和分页参数,例如-

 - xxx. com/services/videos/1/0 
 - xxx. com/services/videos/2/0
 - xxx. com/services/videos/125/6
reactjs react-native fetch-api
1个回答
0
投票

您可以使用 Promise.all。创建一个数组(比方说 url)然后 -

let urls = [
  'xxx. com/services/videos/1/0 ',
  'xxx. com/services/videos/2/0',
  'xxx. com/services/videos/125/6'
];

// map every url to the promise of the fetch
let requests = urls.map(url => fetch(url));

// Promise.all waits until all jobs are resolved
Promise.all(requests)
  .then(responses => responses.forEach(
    // process response here
  ));

或与

await

const texts = await Promise.all(urls.map(async url => {
  const resp = await fetch(url);
  return resp.text(); // or resp.json();
}));
© www.soinside.com 2019 - 2024. All rights reserved.