How to perform elegant assignment in an Axios request?

var txt="33";
this.Axios.post("/api/Event/get_serve_name",data,{headers:{"Authorization":localStorage.gw_token}}).then(res=>{
                console.log(res)
                txt=res.data.HttpData.data[0].txt;
                
            })
return txt;

33
Aug.09,2021

you just deal with it as soon as you get the value in then. There is no need to assign values to external variables.

if you need to write Synchronize, ES6's async/await learn about

.
the stupidest callback assignment return goes out
async/await how long has it been out? learn

Promise

//
function getData(){
    return Axios.post(url).then(res=>res.data.HttpData.data[0].txt)
}
//
getData().then((txt)=>{
    console.log(txt)
})
//async 
async (){
    let txt = await getData();
    console.log(txt)
}

Callback

//
function getData(callback){
    Axios.post(url).then(res=>{
        callback(res.data.HttpData.data[0].txt)
    })
}
//
getData((txt)=>{
    console.log(txt)
})
Menu