title: Callback function on Vuejs
body: <p>I am trying to make a process run in a synchronous way. However, as I have ajax calls, which are naturally asynchronous, I'm having problems because my array is only populated after it has been called. To solve this problem, I'm trying to do a callback function. But I'm not having success.</p>
  
  <p>Here's my code:</p>
  
  <p>(click action calls this function):</p>
  
  <pre><code>VmUser.executa(user,this.addTodos)
  </code></pre>
  
  <p>The functions that are called:</p>
  
  <pre><code>executa(user, callback) {       
    this.montaTodos(user, { complete: callback })
  },
  
  addTodos() {
    const VmUser = this           
    VmUser.details = VmUser.todos.map(user =&gt; {
      VmUser.$bus.$emit('add-user', { user: user })
    })
  },
  
  montaTodos(user) {
    const VmUser = this 
    axios
      .get(`api/gethierarquia/${user.cod_usuario}`)
      .then(res =&gt; {
        if (res.data.usuarios.length !== 0){
          //VmUser.$bus.$emit('add-user', { user: user})
          VmUser.todos.push(user)
          VmUser.supervisores = res.data.usuarios
          VmUser.details = VmUser.supervisores.map(user =&gt; {
            VmUser.todos.push(user)
            axios
              .get(`api/gethierarquia/${user.cod_usuario}`)
              .then(res =&gt; {
                if (res.data.usuarios.length !== 0){
                  VmUser.funcionarios = res.data.usuarios
                  VmUser.details = VmUser.funcionarios.map(user =&gt; {
                    VmUser.todos.push(user)
                  })  
                }
              })
          })
        }
      })
  },
  </code></pre>