I have a question with axios and async functions in general. I can get a return when using a synchronous functions. However, for my async, it returns a promise with fulfilled promise state and undefined promise result. I tried reading other SO questions but I cant seem to understand their explanation.
Sync:
function plusonefunc(var) {
return var + 1;
}
let sum = plusonefunc(1); // returns 2
Async:
async function getvalue(var) {
let data = {
"var": var,
}
await axios({
method: 'POST',
crossDomain: true,
url: '/url',
headers: {
"accept": "application/json",
"Access-Control-Allow-Origin": "*"
},
data: data,
})
.then(response => {
console.log(response); // prints the desired value
return response; // return statement
})
.catch(error => {
console.log(error)
});
}
let response = getvalue("var");
console.log(response); // returns Promise {<pending>}[[Prototype]]: Promise[[PromiseState]]: "fulfilled"[[PromiseResult]]: undefined
How do I get the value of return response
from the axios? Where should I put the return in the response?