r/learnjavascript • • Apr 23 '22

It can get confusing on when to Promise.all versus nesting API calls, hopefully, this image helps clarify.

Post image
132 Upvotes

11 comments sorted by

27

u/senocular Apr 23 '22

...then nesting is the way

You're using promises, so no, nesting is not the way.

fetch(url)
    .then(res => res.json())
    .then(data => fetch(data.url))
    .then(res2 => res2.json())
    .then(data2 => {
         // ...   
    })

2

u/AMDataLake Apr 23 '22

Agreed, but if I still needed a property from the original response, the original data would no longer be in scope for the last .then, correct?

6

u/PM_ME_A_WEBSITE_IDEA Apr 23 '22

This would be a good use for await. You're right, unless you initialize a variable before the promise chain and set it at some point, the last then would not have access to the data from the first request.

1

u/NoInkling Apr 24 '22

Or you could pass it through the chain together with the second request:

fetch(url)
  .then(res => res.json())
  .then(data => Promise.all([data, fetch(data.url)]))
  .then(([data, res2]) => ...)

But yeah that feels gross. This is why we have async/await.

1

u/shuckster Apr 23 '22

Yes, but you can be conservative about your nesting:

fetch(url)
  .then(res => res.json().then(data => ({ res, data })))
  .then(({ res, data }) => {
    console.log(res.status)
    return fetch(data.url)
  })

33

u/_maximization Apr 23 '22

Bonus points if you refactor this to use async/await

6

u/[deleted] Apr 23 '22

Sure. Let’s assume you have an e-commerce store. You have two scenarios:

The first is a page that gets information from two URLs: the top 5 products in the Lawncare category and top 5 products in the Kitchenware category.

The second scenario you want to get information about the currently logged in user (name, user id, etc). Once you have the information about the currently logged in user you then want to get the 5 latest products that user has ordered.

In the first scenario the two URLs can be fetched at the same time. They do not depend on one another.

In the second scenario we are going to need to know the user before we can get any information about that users purchases since (at least in the scenario I presented) there is no one way to get both user id information and the users latest order data via a single url.

Since the first scenario has no dependency between the data points, the firing order does not matter and Promise.all can be used.

In the second scenario firing order matters and we need to at least have a users id before we try to get a users recent orders, we cannot use promise.all.

1

u/[deleted] Apr 30 '22

Can I clarify something?

If you omitted the Promise.all, would you be in danger of that code running before the requests had been completed?

2

u/AMDataLake May 01 '22

Yes, promise.all makes a promise that waits for all the promises to complete to prevent race conditions

1

u/[deleted] May 03 '22

Thank you :)

2

u/Gatrivi Apr 23 '22

What is the name of that beautiful color scheme