I am attempting to ensure that a process runs synchronously. However, the presence of asynchronous Ajax calls has caused issues, resulting in my array being populated only after it is called. In order to overcome this obstacle, I am exploring the option of implementing a callback function. Unfortunately, I have not yet achieved success in resolving this issue.
Here is the snippet of code:
(the click action invokes this function):
VmUser.executa(user,this.addTodos)
The functions being executed:
executa(user, callback) {
this.montaTodos(user, { complete: callback })
},
addTodos() {
const VmUser = this
VmUser.details = VmUser.todos.map(user => {
VmUser.$bus.$emit('add-user', { user: user })
})
},
montaTodos(user) {
const VmUser = this
axios
.get(`api/gethierarquia/${user.cod_usuario}`)
.then(res => {
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 => {
VmUser.todos.push(user)
axios
.get(`api/gethierarquia/${user.cod_usuario}`)
.then(res => {
if (res.data.usuarios.length !== 0){
VmUser.funcionarios = res.data.usuarios
VmUser.details = VmUser.funcionarios.map(user => {
VmUser.todos.push(user)
})
}
})
})
}
})
},