Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  117] [ 4]  / answers: 1 / hits: 53320  / 6 Years ago, fri, july 27, 2018, 12:00:00

I am experiencing an issue where get_user() is running after console.log(usersOutput, 'here'). How can I change it such that get_user() run first?



function get_user(user){
axios.get(`/api/admin/users`,{params:{idnum:user}}).then((user)=>{
console.log('got user')
return user.data
})
}

var UsersFormatter = function(c){
let usersOutput = 'waiting for change'
var usersOutput = get_user(c.cell.row.data.sessionUser)
console.log(usersOutput,' here')
return usersOutput
}

More From » node.js

 Answers
100

You don't make it synchronous, that will block the thread, which you never want to do. Just return the promise from the function and pass the promise around instead of the data:



function get_user(user){
// return this promise
return axios.get(`/api/admin/users`,{params:{idnum:user}}).then((user)=>{
console.log('got user')
return user.data
})
}

var UsersFormatter = function(c){
// return this promise too, so callers of UserFormatter can get the data
return get_user(c.cell.row.data.sessionUser)
.then((data) => /* format data and return */)
}

[#53861] Wednesday, July 25, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gerardob

Total Points: 571
Total Questions: 115
Total Answers: 96

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;