1

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()
}
LeBlaireau
  • 17,133
  • 33
  • 112
  • 192

1 Answers1

2

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)
   }
}
Nicolas Takashi
  • 1,550
  • 2
  • 10
  • 11