1

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?

Axel
  • 4,365
  • 11
  • 63
  • 122
  • 1
    Because the second request depends on knowing the response to the first request, it looks like you're doing it right, though an option would be to get `/users` regardless and use some kind of `Promise.all` to avoid having to wait twice. Quicker resolution, more unnecessary requests. – CertainPerformance Sep 30 '18 at 03:55
  • Thanks @CertainPerformance for the reply! Do you mean to get `/users`, without checking the response of the first request? – Axel Sep 30 '18 at 04:00
  • This code "smells" bad... why do you have to validate a token before making a request with it? Why not just make the request and handle the failure case if the token wasn't valid yet? How would you handle the situation where the token becomes invalid between the first and second requests? – Brad Sep 30 '18 at 04:04
  • @Brad! When the token becomes invalid, I redirect the user to the intended page. I like your approach though. Let me try that right away. – Axel Sep 30 '18 at 04:14
  • You should look at pros/cons. Using `promise.all` makes 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. – iagowp Sep 30 '18 at 04:17
  • @iagowp Got it! Thank you. I guess I will go with handling all the validation and getting stuff in one request only. – Axel Sep 30 '18 at 04:20

2 Answers2

2

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?

The approach which you are currently using is right!

As we can see, your application logic has to be executed sequentially.

First, validating the token

If the token is valid, then allow accessing the server's resource which is the list of users.

If you use Promise to handle these asynchronous operations, you also need to chain those promises in order to implement the application logic.

You Nguyen
  • 9,961
  • 4
  • 26
  • 52
0

What you are doing looks right for the problem you have. However the chaining becomes cumbersome when the number of activities you need to do in a chain increases.

You could also try out async waterfall in such a case. https://caolan.github.io/async/docs.html#waterfall

Mohit Mutha
  • 2,921
  • 14
  • 25