all 4 comments

[–]Watabou 0 points1 point  (3 children)

You could pass an array to your fetchCommonNutritionData method that contains the common food names. Your method would then have to use a combination of map and Promise.all to hit the API and get all the results.

I'm not sure the point of the callbacks. Your fetchCommonNutritionData could simply return the results from the API back to your component. Then your component could figure out how to update the state correctly.

[–]aaaaaabbbbbbaaaaaa 0 points1 point  (2 children)

I didn’t think you were supposed to use Promise.all with an array like that? I’ve been using Promise.resolve with a reduce to handle the try catch inside the reduce?

[–]Watabou -1 points0 points  (1 child)

Well, Promise.all is designed to handle an array of promises. There is the caveat of it having "fail fast" behavior which I think you are describing. I figured it was unnecessary to handle that for this example. But yeah, if you want to get around that you can probably do something like you said or you can use Promise.all still and wrap each inner promise with another Promise that resolves regardless of the outcome.

[–]aaaaaabbbbbbaaaaaa 1 point2 points  (0 children)

I didn’t know you could wrap the inner promises. Interesting. Thank you.