How i can fetch data in react native with foreach or something?

Viewed 62

I have this api url xxx.com/services/videos/xx/yy

xx = category param
yy = pagination param

I want foreach this category and pagination params, for example -

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

You can use Promise.all. Make an array (let's say urls) & then -

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
  ));

or with await

const texts = await Promise.all(urls.map(async url => {
  const resp = await fetch(url);
  return resp.text(); // or resp.json();
}));
Related