[Vuejs]-Why use a promise when making an axios api call?

3👍

With promises you can call asynchronous functions.
e.g. here when you want to use loadCustomer you can await until this function resolve or reject:

try {
  // resolve
  const response = await loadCustomer()
} catch(err) {
  // reject
  console.log(err)
}

axios it self return a promise:
so you can rewrite your function like this:

loadCustoemr() {
  return axios.get(this.DetailsDataUrl)
}

and call it:

loadCutomer()
  .then(res => this.Customer = res.data)
  .catch(err => console.log(err))

as above you can also use async/await here.
for more information you can use this link,

Leave a comment