There are two mandates when it comes to Asynchronous JavaScript
- Reduce unnecessary requests
- Reduce wait time as much as possible
Reducing unnecessary requests is easy — just don’t fetch information that the user doesn’t require.
We’re going to focus on the second mandate as it’s a bit more complex.
Reducing wait times
You can reduce wait times using two methods:
Batching requests when you can
When you send a request, you need to wait for a response from the server.
How fast the response comes back depends on many factors such as:
- Your internet speed
- The latency
- How far the server is away from you
- Whether the server uses a CDN
- How large is the data
Most of the time, you’ll get a noticeable lag time between a request and a response. So you should always batch requests together when you can.
This lag time might not exist when you’re fetching from localhost since
you’re fetching from your own computer.
How to batch requests
You can batch requests with a method called Promise.all.
Promise.all takes in an array of promises.
When all promises are fulfilled, you can get the respective values in then.
Promise.all([promise1, promise2]) .then(values => { const [valueOne, valueTwo] = values console.log(valueOne) console.log(valueTwo) })Any error from any promise within Promise.all will send the code into a catch call. You can then handle the error like a normal promise.
Promise.all([promise1, promise2]) .catch(error => { /*Handle error*/ })What happens when you batch requests
We can create a fake request to demonstrate the difference between batching requests and not batching requests.
Here, we are going to do a few things:
- We’re going to create a
requestPromise that mocks afetchfunction. - This
requestwill have a 1-second lag time. - We’ll write two sets of code code — one that batch requests and one that doesn’t.
- Then we’ll compare the time it takes to get the response.
First, let’s create the request function with a 1-second lag time.
We can use setTimeout to make this function.
To make things simple, we will make the response value the same value that was passed into the request.
// Promises that let us create a delayfunction delay(duration) { return new Promise(resolve => { setTimeout(resolve, duration) })}
// Returns a promise with a value after 1 secondfunction request(value) { return delay(1000).then(() => { return value })}If you use this request function, you should now see a time lag of 1 second.
request('something').then(value => console.log(value))Next, let’s write two sets of code:
- We’ll use
requestto generate two requests. - In the non-batching code, we’ll use
awaitto wait for the first request to finish before we send the second request. - In the batching code, we’ll use
Promise.allto send both requests together.
Here’s the non-batching code with await.
const one = await request('one')console.log(one)
const two = await request('two')console.log(two)For this code, you’ll notice it takes 2 seconds to for all the requests to be complete.
Now here’s the batching code with Promise.all.
Promise.all([request('one'), request('two')]).then(values => { const [valueOne, valueTwo] = values console.log(valueOne) console.log(valueTwo)})For this code, you’ll notice it only takes 1 second to complete both requests.
That’s all you have to know about batching requests.
We’ll talk about Optimistic UI in the next lesson.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson