Answer the question
In order to leave comments, you need to log in
How to correctly run an asynchronous function from a service?
Good afternoon. There is a service with a function that loads information from the database.
function getData($http) {
this.users = async function(){
const connect = await $http.get('http://localhost:3000/getUsers')
const data = await connect.data
return data
}
}
function MainCtrl($scope,$rootScope,$log,getDataServis, $http){
const vm = this
getDataServis.users()
.then((res,rej)=>{
vm.arr = res
})
vm.arr = (getDataServis.users())
vm.clickFunction = function(name){
console.log('hi')
}
vm.arr = (getDataServis.users())
, in the application, data from the database appears after pressing the button with the function vm.clickFunction
. If you leave the line, first an empty object {} appears in the application, and then the information itself. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question