Requesting many resources at once
You may find yourself requesting many resources at once. This lesson will show you how to make such requests with Fetch and zlFetch.
Weโre going to keep it simple (and conceptual).
Letโs say you want to serve a meal. To serve this meal, you need to fetch three resources. All three resources must be ready before you can serve the meal.
The three resources are:
- A burger
- A pack of fries
- A drink
(Substitute burger, fries and drink with three GitHub requests if you want a real example).
Promise.all
The Promise
constructor has a method called all
. all
takes in an array of promises. It waits for all promises to resolve or reject calling then
or catch
.
Promise.all([promise1, promise2, promise3])
.then(responses => { /* Do something with the responses */ })
.catch(error => { /* Do something with the errors */})
The argument sent to then
or catch
is an array from each promise.
Promise.all([promise1, promise2, promise3])
.then([response1, response2, response3] => {
// Do something with each result
})
Hereโs what our code might look like if we use promises to fetch burger
, fries
and drink
.
const burgerPromise = getBurger()
const friesPromise = getFries()
const drinkPromise = getDrink()
const serveMeal = Promise.all([
burgerPromise,
friesPromise,
drinkPromise
])
.then([burger, fries, drink] => {
console.log(`${burger} ๐`)
console.log(`${fries} ๐`)
console.log(`${drink} ๐น`)
})
Requesting many resources with Fetch
You have two ways to fetch burger, fries and drink with Fetch.
Method 1
The first method is to call three Fetch requests with Promise.all
.
In the first then
call, you need to transform each response into JSON. Since response.json
returns a promise, you need to use another Promise.all
.
const burgerPromise = fetch('burger link')
const friesPromise = fetch('fries link')
const drinkPromise = fetch('drink link')
Promise.all([
burgerPromise,
friesPromise,
drinkPromise
])
.then(([burgerResponse, friesResponse, drinkResponse]) => {
return Promise.all([
burgerResponse.json(),
friesResponse.json(),
drinkResponse.json()
])
})
.then(([burger, fries, drink]) => {
console.log(`${burger} ๐`)
console.log(`${fries} ๐`)
console.log(`${drink} ๐น`)
})
Method 2
The second method is to make the first then
calls when requesting each resource.
Then, use Promise.all
to handle the responses. Each Fetch request will resolves their first then
call when theyโre ready.
const burgerPromise = fetch('burger link')
.then(r => r.json())
const friesPromise = fetch('fries link')
.then(r => r.json())
const drinkPromise = fetch('drink link')
.then(r => r.json())
Promise.all([
burgerPromise,
friesPromise,
drinkPromise
])
.then(([burger, fries, drink]) => {
console.log(`${burger} ๐`)
console.log(`${fries} ๐`)
console.log(`${drink} ๐น`)
})
Requesting many resources with zlFetch
zlFetch writes the first then
call for you. This means that youโll use method 2 automatically. However, zlFetch stores the response body inside the body property. So youโll need to use this property to obtain the data.
Promise.all([
zlFetch('burger link'),
zlFetch('fries link'),
zlFetch('drink link')
])
.then(([burgerResponse, friesResponse, drinkResponse]) => {
console.log(`${burgerResponse.body} ๐`)
console.log(`${friesResponse.body} ๐`)
console.log(`${drinkResponse.body} ๐น`)
})
Exercise
Do this:
- Make three different requests at once with Fetch
- Make three different requests at once with zlFetch or Axios