Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make axios call synchronous? [duplicate]

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
}
like image 711
ApplePie Avatar asked Jul 27 '18 18:07

ApplePie


People also ask

Is Axios asynchronous or synchronous?

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.

How do I create multiple Axios calls?

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.

Is Axios asynchronous?

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.

How do I return promises from Axios?

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.


2 Answers

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
}
like image 137
Aldo Sanchez Avatar answered Nov 01 '22 14:11

Aldo Sanchez


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 */)
}
like image 31
Mark Avatar answered Nov 01 '22 14:11

Mark