I have the following:
async fetch() {
const res = await axios.get('/api/validate/randomtoken')
if(res.exists) {
await axios.get('/api/users')
}
}
As you can see, I first validate the token, and return back all users. Here I am using two await's.
As per this question: How run async / await in parallel in Javascript, I should use Promise.all(). However, the problem is I need to check if res.exists compulsorily, so I guess I can't use Promise.all()!
I just wanted to know that if the approach which I am currently using is right or not and also if possible how could I use Promise.all() here?
/usersregardless and use some kind ofPromise.allto avoid having to wait twice. Quicker resolution, more unnecessary requests./users, without checking the response of the first request?promise.allmakes your code more complex, but makes it faster for users with the right credentials, and slower for user without it. The approach you are using, code looks better, but it will be slower for users with correct credentials and faster for user who will be redirected.