0

I have three ajax function with promise, in last function there is loop after that I need to run the function

function requestToken() {
  return ajax
} 
function getUserInformation(token) {
  return ajax
}
function getManager(token, userid){
  return ajax
}

Now I am calling as below

requestToken().then(function(token) {
      getUserInformation(token.access_token).then(function(_users) {
        var users = _users.value,
          userArray = []
        //console.log(users)
        users.map(user => {
          getManager(token.access_token, user.id)
            .then(function(manager) {
              userArray.push({
                id: user.id,
                image: getPhoto(user.mail),
                name: user.displayName,
                label: user.jobTitle,
                managerid: manager.id,
              })
            })
            .fail(function(err) {
              userArray.push({ id: user.id, image: getPhoto(user.mail), name: user.displayName, label: user.jobTitle, managerid: null })
            })
        })
        //console.log(userArray)
        nullArray = Enumerable.from(userArray)
          .where(function(value) {
            return (userArray.managerid = null)
          })
          .toArray()
        console.log(nullArray.length)
      })
    })

I am getting nullArray length as zero. I know nullArray is running before my loop finishes. How can we run after loop

Milind
  • 1,855
  • 5
  • 30
  • 71

0 Answers0