I am trying to get a basic async/await working with Axios, any pointers would be helpful.
isUserInDatabase() {
axios.get(url)
.then( (response) => {
return response.data.data;
})
},
async isUnique() {
await this.isUserInDatabase()
}
The current problem is: You resolve your promise and don't return a new promise in isUserInDataBase method.
Try some thing like that:
isUserInDatabase() {
return axios.get(url);
}
async isUnique() {
try {
await this.isUserInDatabase()
} catch(error) {
console.log(error)
}
}
OR
isUserInDatabase() {
return new Promise(async (resolve,reject)) => {
try {
const result = await axios.get(url);
resolve(result.data.data)
} catch (error) {
reject(error)
}
})
},
async isUnique() {
try {
await this.isUserInDatabase()
} catch(error) {
console.log(error)
}
}
来源:https://stackoverflow.com/questions/51554461/async-await-in-axios