0

The problem is that when i get responses, they are not in the right order, for example, I get 1.name, 3.name, 4.name, 2.name, 5.name. How can I get responses in right order, like in devIds array?

var devIds = ['1', '2', '3', '4', '5']


devIds.map(function(id){                   
    DeviceService.getDevInfo(id).then(function(response){
         console.log(response.name)
       })
  })

I had pretty much the same problem in React but I fixed it like this

axios.all(array.map(el => axios.get(`/item/${el}`)))
     .then(data => console.log(data));

Here I am trying something like this and it doesnt work

Promise.all(devIds.map(function(el){DeviceService.getDevInfo(el)}))
     .then(function(res){console.log(res)});
  • 2
    https://docs.angularjs.org/api/ng/service/$q#all – JB Nizet Apr 20 '19 at 17:30
  • 1
    Note: saying "it doesn't work" is not helping us to help you. Say precisely what you expect to happen, and what happens instead. – JB Nizet Apr 20 '19 at 17:33
  • Sorry, in developer tools in network I see that all http requests are done, but somehow I do not get that responses in console log that is in than(). As i wrote above, i expect to get responses in order like in the array. But I dont. – SecondClassCitizen Apr 20 '19 at 17:37
  • 1
    Use the $q.all service, as I linked to in my first comment. – JB Nizet Apr 20 '19 at 17:39
  • 1
    You forgot to `return` when you went from arrow functions to standard ones. Therefore, you've incorrectly assumed that the problem is in API differences, when in fact all of those promise based apis behave the same way in this respect and the actual problem is the syntactic context in which you use those apis – Aluan Haddad Apr 20 '19 at 18:57
  • @ Aluan Haddad Thanks, you were wright. – SecondClassCitizen Apr 20 '19 at 19:52

0 Answers0