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?

6
  • 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. Commented Sep 30, 2018 at 3:55
  • Thanks @CertainPerformance for the reply! Do you mean to get /users, without checking the response of the first request? Commented Sep 30, 2018 at 4: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? Commented Sep 30, 2018 at 4: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. Commented Sep 30, 2018 at 4: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. Commented Sep 30, 2018 at 4:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Uhhh, there's nothing cumbersome about chaining using await. It's just normal looking sequential code. I see no reason to involved async.waterfall at all here.
asyncjs with promises is more cumbersome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.