I am currently exploring the usage of axios (https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.0/axios.js) in a function to communicate with my backend and retrieve data.
The snippet of code provided is functional.
Instead of relying on a global variable to store the response, I am seeking an alternative method.
However, when attempting to move the variable myresponse into the function itself, I encounter difficulties capturing the output.
Is there a way to achieve this without resorting to using a global variable?
var myresponse = "xxx"
function getTimeData() {
axios.get('/time')
.then(function (response) {
console.log(response.data);
myresponse = response.data;
})
.catch(function (error) {
myresponse = "error";
});
return myresponse;
}
console.log(getTimeData())
In this scenario, a local server running at "/time" returns a timestring.