Clicking the Click Me
button calls the getDataAsObservable
method defined in http.service.ts
script:
export class HttpService {
jsonFile = '../assets/products.json';
constructor(private http: HttpClient) {}
getDataAsObservable(): Observable<any> {
return this.http.get(this.jsonFile)
}
}
After receiving the observable
from the http
service the requestDataAndWait
method defined in the app.component.ts
script assigns the received response
to the local variable data
and returns it back to the onClick
method. But instead of receiving the array data the onClick
method gets ZoneAwarePromise
object:
async requestDataAndWait() {
let data = [];
await this.http.getDataAsObservable()
.toPromise().then(response => {data=response;} );
return data
}
Here is the link to the Stackblitz project: https://stackblitz.com/edit/angular-ivy-bygaut?file=src%2Fapp%2Fapp.component.ts
How to make the requestDataAndWait
return the data
array instead of the ZoneAwarePromise
object?