This question already has an answer here:
- How do I return the response from an asynchronous call? 35 answers
I can't return a value using this function because it is empty.
getNameById (id) {
var name = ''
axios.get('/names/?ids=' + id)
.then(response => {
this.response = response.data
name = this.response[0].name
})
.catch(e => {
this.errors.push(e)
})
// Is empty
console.log('Name ' + name)
return name
}
How do I access the name variable inside "then" and return it?