1

can someone help me with this?

To avoid getting 429s errors, I need it to only run the map for the next item of the array once the result has been returned.

Te code below is logging:

START 632
START 634
START 636
Return from API inside getDifferences() [ 1057285 ]
Return from API inside getDifferences() [ 1057287 ]
Return from API inside getDifferences() [ 1057283 ]
FINISH [ 1057285 ]
FINISH [ 1057287 ]
FINISH [ 1057283 ]

I would like it to be like:

START 632
Return from API inside getDifferences() [ 1057285 ]
FINISH [ 1057285 ]
START 634
Return from API inside getDifferences() [ 1057287 ]
FINISH [ 1057287 ]
START 636
Return from API inside getDifferences() [ 1057283 ]
FINISH [ 1057283 ]

Code:

const differences = await Promise.all(products.map(async product => {

    console.log('START', product)
    const result = await getDifferences(product) // api 
    console.log('FINISH', result)
    
    return result

}))
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • `Array.prototype.map()` is not await-aware. You can achieve what you want with a `for` loop. – Roamer-1888 Sep 12 '20 at 08:19
  • Note that promises are the _results_ of async operations so `Promise.all` doesn't actually control concurrency in any way, shape or form. In order to run promises in a sequence the most common way is with a loop, I'll find a duplicate for you. Happy coding. – Benjamin Gruenbaum Sep 12 '20 at 10:29

0 Answers0