0

this is my first time to develop a react/node js application.

The current hooks setup to call api is the line of code below.

The current setup is that application use Promises.all to call all promise that invoke axios.post and runs in parallel. The browser threads pool with default 5 concurrency level in chrome and starts other request if one of the 5 requests in the pool finished earlier.

But there is a change in requirements that before invoking axios.post, the numbers must be arranged in ascending and be called in sequential order.

Original     Api Call
numbers      runs in parallel
Input      
1            145 Thread1
3            32  Thread2
4
2
5

After Change Api Call
numbers      runs in sequence
Input      
1            12345 Thread1
3            
4
2
5

const doReprocess = (numbers= []) => {

    txnSeqs.forEach(async numbers=> {
      setIsReprocessing(numbers, ReprocessStatus.RUNNING);
      try {
        const result = await axios.post(apiBaseUrl + numbers, {}, {
          headers: {
            'Content-Type': 'application/json'
          }
        });
        setIsReprocessing(numbers, ReprocessStatus.SUCCESS, result);
      } catch (err) {
        setIsReprocessing(numbers, ReprocessStatus.ERROR, err.response.data || err);
      }
    });
  }


Is there a way to do this approach? And can you explain it in detail how this can happen?

Thank you

Dan
  • 559
  • 4
  • 9
  • 23
  • Instead of using `forEach` you can do -> `for (const numbers of txnSeqs) {` instead.. – Keith Jun 21 '19 at 10:14
  • Please elaborate. Do you want to make the requests in parallel or sequence ? You current code snippet will do it in sequence as `await` will block the loop. – Easwar Jun 21 '19 at 10:16
  • 1
    @Easwar It won't do them in sequence, the await inside a `forEach` does not block the loop, as forEach is not async aware, but `for of` is.. – Keith Jun 21 '19 at 10:17

0 Answers0