How to use async/await with .map in js
At some point you may have wondered how to use asynchronous functions in methods like .map or .fo...
For further actions, you may consider blocking this person and/or reporting abuse
Could you help me explain what happen if there is a fetch request is failed in using Promise.all?
yes, it is a good question I will give you an example that will solve your doubt.
In the promise.all if a call fails then it will not enter the then but it will go directly to the catch.
But if you need it to return everything with its states you can use Promise.allSettled which will return an array with the status of each of the promises
script log
Agree with you @jordan Jaramillo. Btw, we can also use Promise.all to resolve this problem. Assume that we're fetching many requests. If one of them is successful, we'll return:
Otherwise
How about your thought?
The problem with promise.all is that you will not be able to access which ones were resolved correctly since it directly passes to the catch.
I mean that if any request is failed, we still use
resolve
instead ofreject
. We only change the returned status.yes, you can do it that way, it's a good idea I hadn't thought about it but if you want to have it automatically you can use allSettled.
Agree !!!
I have one question when I will use Promise.all and for of loop with async-await 🤔
A case based on real work might be that you have an array of filenames and you want to get the data from each file asynchronously, in this case you would use Promise.all since the order doesn't matter and one doesn't depend on the other.
But what happens if in order to access the information of the next one you need to have the information of the previous one since they asked you to do so, then you must use for of since it is executed sequentially here.
To expand on your file fetching example. You would use the for...of if you wanted to concatenate a set of files. For example: concatenate f1.txt f2.txt.
The sequence must then be f1.txt followed by f2.txt.
If promise.all was used and f2.txt was a small file while f1.txt was a huge file, then the concatenation would have the incorrect sequence.
PS I haven't tried this but will.
Exact!
It really helps a lot. Thanks for sharing and your efforts 🙌🙌.
Thank you so much 🙌🤝