我有这个 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
您可以使用 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();
}));