-3

i have an array of urls and i'm trying to get and display the status for every specific website inside a list. Any ideas for the best approach in react?

const URL = [
  {
    name: 'site1',
    url: 'https://something'
  },
  {
    name: 'site2',
    url: 'https://something'
  },
  {
    name: 'site3',
    url: 'https://something'
  }

]
  • 1
    Welcome to Stack Overflow! Please read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Sep 27 '19 at 10:08
  • *"i'm trying to get and display the status for every specific website inside a list"* Note that unless all of those websites allow you to query them cross-origin via CORS (unlikely, but if you control them, possible), you can't do this purely client-side. You'll need to make the request to *your* server, which can query those sites, then send you the response. More: [SOP](http://en.wikipedia.org/wiki/Same_origin_policy). – T.J. Crowder Sep 27 '19 at 10:10
  • 1
    Hi T.j, thanks for the feedback. in this case i can control them. – sleepwalker127 Sep 27 '19 at 10:20

2 Answers2

-1

Please use the Promises.all.The Promise.all() method returns a single Promise that resolves when all of the promises passed as an iterable have resolved or when the iterable contains no promises

 const urls = [{ name: 'site1', url: 'https://something' },
{ name: 'site2', url: 'https://something' },
{ name: 'site3', url: 'https://something' }];

Promise.all(urls.map(item => fetch(item.url)
.then(res => res.json()).catch((ex) => ex)))
.then((values) => console.log(values));
Kiren Paul
  • 91
  • 1
  • 12
-1

You just need to do loop over list with map operator, below is the stackbliz example which may help you. https://stackblitz.com/edit/array-map-bjaytm?file=index.js

nitinsingh9x
  • 632
  • 6
  • 12