use promise in angular 8 1

use promise in angular 8

  private fetchData(){
    const promise = this.httpClient.get(this.apiUrl).toPromise();
    console.log(promise);  
    promise.then((data)=>{
      console.log("Promise resolved with: " + JSON.stringify(data));
    }, (error)=>{
      console.log("Promise rejected with " + JSON.stringify(error));
    })
  }

Here is what the above code is Doing:
1. We are using the HttpClient service to make a GET request to the API.
2. We are using the toPromise() method to convert the Observable to a Promise.
3. We are using the then() method to subscribe to the Promise.
4. We are using the catch() method to handle errors.

Similar Posts