0

I've the following code which use async to get data from two functions which is call to some back-end to get data, the code works.

    async function ac(token, gl) {
        for (const acc of gl) {
            let subsReqEu = sub(token, acc.guid, reg.1);
        
            let subsReqUs = sub(token, acc.guid, reg.2);
            // run both function async in parallel go get data from the backend
            const [se, su] = await Promise.all([axios(subsRE), axios(subsRU)])
        
          // here I got the response from the promise and should loop on it got get URL
            for (const sub of se.data.sub) {
                if (sub.url) {
                    return sub.url
                }
            }
           // the same for the second response of the promise all
            for (const sub of su.data.sub) {
                if (sub.url) {
                    return sub.url
                }
            }
    }

The await Promise.all return the data. Now I need to loop on each of the returned value and find url and exit from the function, currently the loops are running in sequence,(assume I've Thousands of data one each loops) how should I run them im parallel (the two for statement) , as the first get result (url) will return the function with the value

Beno Odr
  • 1,123
  • 1
  • 13
  • 27
  • Does this answer your question? [Asynchronous Process inside a javascript for loop](https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) – Liam Jul 29 '20 at 11:19
  • @Liam - no as my code after the promise is not asyc , its just sync code – Beno Odr Jul 29 '20 at 11:20
  • You need to be careful of your usage or parallel and asynchronous, they're not the same thing – Liam Jul 29 '20 at 11:20
  • @Liam - yes I know. I run the async code , now I want to run the sync code (for loop) in parallel, how can I do it? – Beno Odr Jul 29 '20 at 11:21
  • so how does the consumer of this method get the results? At some point someone is going to have to wait – Liam Jul 29 '20 at 11:21
  • @Liam the `ac` function called `var res = await ac(var1,var2)` , does it answer your question? – Beno Odr Jul 29 '20 at 11:24
  • this is all very strange. Even if you could run those `for` async, which you can't as they don't do an IO why would you want to? – Liam Jul 29 '20 at 13:03
  • I think you want something like [`promise.race`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race). I started writing an answer but the more I read your code the less sense it makes. – Liam Jul 29 '20 at 13:07
  • @Liam - what doesnt make sense ? please explain! I run two async call to backend to get data, and then loop on it and when find url exit ? what is the issue with the code ?how would you do it ? – Beno Odr Jul 29 '20 at 15:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218834/discussion-between-beno-odr-and-liam). – Beno Odr Jul 29 '20 at 15:48

0 Answers0