在返回值之前等待回调?

时间:2016-04-26 12:58:07

标签: javascript meteor callback

我在函数内部启动了几个回调函数。我不希望父函数在所有回调函数返回它的响应之前返回任何值。

我试过这个,没有任何运气:

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
}

知道如何使这项工作吗?

0 个答案:

没有答案
相关问题