I created an array using some data then I tried to copy it to the useState array. But it doesn’t work. Also, I viewed it using the console log. but line 49 logged before line 36. can I fix this?
Let’s say you have a list of IDs ids.
Let’s say you have an asynchronous function fetchDocument that takes a single ID, and returns a promise that evaluates to some data.
Let’s say you want to fetch the document for every ID in the list and then log the entire list of documents.
That could look like this:
I feel I should point out that there is one big difference to these two approaches though. With Promise.all() the fetches will be run at the same time in parallel. With await inside a for loop it will run sequentially, waiting for the first fetch to finish before starting the second, waiting for that to finish before moving onto the third, and so on. In theory, the parallel processing with Promise.all() should run faster so I would prefer it when parallelization is possible.
And sometimes you need sequential processing in which case Promise.all() is out, so @ravinduonline take notes, you’ll probably end up using both of these methods in your career.
(getting sidetracked here but…) Also, this is a pretty new API but they recently added Promise.allSettled() which works similarly but waits for all promises to finish, whether they succeed or fail. Haven’t got a chance to use it yet though.