我如何获取数据在react原生与foreach或什么?



我有这个api urlxxx.com/services/videos/xx/yy

xx = category param
yy = pagination param

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

- xxx. com/services/videos/1/0 
- xxx. com/services/videos/2/0
- xxx. com/services/videos/125/6

你可以使用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();
}));

相关内容

  • 没有找到相关文章

最新更新