Skip to content Skip to sidebar Skip to footer

In Angular, How To Handle Promise Reject When Using Async/await

In Angular, if I use promise, the code would be let promise = this.$resource('www.example.com.au/request.json').get().$promise promise.then(data => { //promise solved }, ()

Solution 1:

If the promise is rejected, an error will be thrown. Use try...catch:

async getData() {
  try {
    let data = await this.$resource('www.example.com.au/request.json').get().$promise
    this.localData = {...data};
  } catch(error) {
    // promise rejected
  }
}

Post a Comment for "In Angular, How To Handle Promise Reject When Using Async/await"