0

I am initiating several callback functions inside a function. I don't want the parent function to return any value before all the callback functions have returned it's response.

I have tried this, without any luck:

function getList(userList) {
  let tempArray = []
  let allAboard = false

  userList.map((users, usersIndex) => {
    users.map((user, userIndex) => {
      HTTP.get(SOME API CALL, function(response){
        tempArray.push({'user': user.id, 'elements': response.data})
        if (tempArray.length === (userList.length * users.length)){
          allAboard = true
        }
      })
    })
  })

  while(!allAboard) {
    if (allAboard) {
      break
    }
  }

  return tempArray
}

Any idea how to make this work?

allegutta
  • 5,626
  • 10
  • 38
  • 56
  • Promises. For details answer need to know what HTTP is? – dfsq Apr 26 '16 at 13:01
  • Yes,be more specific with the `tags` you set for your question: are you using Angular2? The environment you go, the callback handling you find... :-) – MarcoS Apr 26 '16 at 13:03
  • @MarcoS I am not using Angular. I am using Meteor and this is on the server side :) – allegutta Apr 26 '16 at 13:05
  • Good. I recommend you read this blog post: https://blog.tableflip.io/promises-in-meteor/ – MarcoS Apr 26 '16 at 13:06
  • please use Meteor.wrapAsync() call this please have a look on this https://themeteorchef.com/snippets/synchronous-methods/ – Pankaj Jatav Apr 26 '16 at 18:13
  • Thanks for all your advices! I ended up with using async/await <- it is AWESOME! Thanks @corvid :) – allegutta Apr 27 '16 at 09:54

0 Answers0