0

I have an Set array which contains multiple Ids.I would like to loop through the Set and make the api calls in parallel for each id and get back the user object, add it to a map.How can i achieve it.

The value is Set userIds :Set[2] 0:"1" 1:"2"

 data() {
   return {
    userIds: new Set(), 
  };
},

 const res = getUsers(userId) 
3
  • write declarative code instead of messy code. as you are thinking code will be messy. since javascript is asynchronous so it is achievable. Commented Mar 11, 2021 at 1:23
  • you can check out stackoverflow.com/questions/53438119/… Commented Mar 11, 2021 at 1:33
  • I would like to call same api in parallel with multiple ids.The above answer you referred is different. Commented Mar 11, 2021 at 1:38

1 Answer 1

0

hope it will resolve your issues. i did not test, just writing code here directly.

    // set requests
    let allRequests = []
    //you can use other loop based on your decession
    this.userIds.forEach(id => { allRequests.push(axios.get(`your_url\${id}`)) })
    
    // you can use await it is based on you requirement
    axios.all(allRequests).then(axios.spread((...responses) => {
      //make your map here using responses
    })).catch(errors => {
      // react on errors.
    })

you can check this reference

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

Comments

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.