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
}
Axios is a promise-based HTTP client for Node. js and the browser. Sending asynchronous HTTP queries to REST endpoints and performing CRUD operations is simple using Axios POST request and GET request. It can be used directly in JavaScript or in conjunction with a library like Vue or React.
Since axios returns a Promise we can go for multiple requests by using Promise. all , luckily axios itself also ships with a function called all , so let us use that instead and add two more requests. Again we define the different URLs we want to access. const requestOne = axios.
Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.
You have to wait for it, then the promise will pass it to your callback: When using async / await, the async function will always return a promise. In your code, whoever is the final user of Cases() will need to wait for and consume the data as a promise if the data itself is the result of an asynchronous activity.
You can make use of the axios promise and use async/await. So something like this:
function get_user(user){
return axios.get(`/api/admin/users`,{params:{idnum:user}})
}
var UsersFormatter = async function(
let usersOutput = await get_user(c.cell.row.data.sessionUser)
console.log(usersOutput,' here')
return usersOutput
}
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 */)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With