0

I have a list of sites and I want each one of them with Axios, but it seems that the order of the requests is respected, besides that not all of them fall into the catch.

What is the best way to perform a sequence of requests?

Code:

async function searchContent(url) {
  try {
    let request = await axios(url);
    let content   = await request.data;
    return content;
  } catch (e) {
    return e;
  }
}

async function start() {
    sites.forEach(async (item)=>{
        let content = await searchContent(`https://${item["URL"]}`);
        //DEBUG
        console.log(content)
    })
}

Error (Error is long, so 3 prints):

https://i.stack.imgur.com/7PXMh.png

https://i.stack.imgur.com/KB0Gr.png

https://i.stack.imgur.com/tMN8M.png

Boruto
  • 39
  • 4
  • looks like you have a rogue `"` before `https://` – bherbruck Jun 10 '22 at 16:13
  • And just as a sidenote: `Array::forEach` doesn't wait for `async` callbacks. It *won't be possible* to wait until all requests have finished ... – derpirscher Jun 10 '22 at 16:19
  • @bherbruck, I fixed it but it still returns the same errors – Boruto Jun 10 '22 at 16:21
  • Are you sure there is some process listening at 127.0.0.1 at port 80? The error says: no. Or your requests may also be blocking the server. In both cases, the error is in the backend and not in the frontend ... – derpirscher Jun 10 '22 at 16:22
  • @derpirscher, If ForEach doesn't wait, then will Axios try to do all requests at once? – Boruto Jun 10 '22 at 16:23
  • More or less, yes ... – derpirscher Jun 10 '22 at 16:23
  • @derpirscher, My list contains 8000 URL's, so ForEach is going through the list, sending the request, but not waiting for a request to finish to request another, so Axios is doing practically all of them at the same time, is that it? – Boruto Jun 10 '22 at 16:26
  • Is there any way around this? – Boruto Jun 10 '22 at 16:27
  • you could for instance do like this https://stackoverflow.com/questions/37213316/execute-batch-of-promises-in-series-once-promise-all-is-done-go-to-the-next-bat – derpirscher Jun 10 '22 at 16:29
  • https://stackoverflow.com/q/37576685/15048478 – Timur Jun 10 '22 at 16:36
  • @Boruto you want to do them one by one? One request, wait for response, then the next request, and so on? – TKoL Jun 10 '22 at 16:42

0 Answers0