0

I have JavaSctipt Array of objects, like

var companies = [{name:'A', userIds: [23,45], tel: 3434343434}, {name:'B', userIds: [98,568,656,54], tel: 554556445}];

function flat() {
   return companies.map(function(cmp) {
     return { 
      n: cmp.name 
      // xmlhttp remote call and callback to fetch users with these ids
      users: getUsersforThisOrg(cmp.userIds, function(usrs) { return usrs } 
     }
   });
}
var companiesWithUsers = flat();

Expected output of companiesWithUsers is

[
 {n:'A', users: [{username: 'user1', name: 'John Doe', ...]}, ...]
]

But it returns

[
   {n:'A', users: undefined}, ...]
]

it means map function returns before getUsersforThisOrg returns the remote result, is there any workaround to wait and append users from remote server.

Wimal Weerawansa
  • 309
  • 2
  • 15
  • 33

0 Answers0