There are three buttons:
Clicking the first Request HTTP Data As Promise button gets its HTTP response as a Promise.
The second Request HTTP Data As Observable button gets its response as an Observable.
Both these buttons get their responses using asynchronous response mechanism.
Now, I would like the third Request HTTP Data and Wait button to get a synchronous response. I would like it to wait for the http service to return the HTTP response.
How could it be done? Here is the link to the Stackblitz project (please use the placeholder function getDataSynchronous defined in the HttpService script to implement this functionality):
https://stackblitz.com/edit/angular-ivy-ukgwct?file=src%2Fapp%2Fhttp.service.ts
export class HttpService {
jsonFile = '../assets/products.json';
constructor(private http: HttpClient) {}
getDataAsPromise(): Promise<any> {
return this.http.get(this.jsonFile)
.toPromise()
}
getDataAsObservable(): Observable<any> {
return this.http.get(this.jsonFile)
}
getDataSynchronous(): any {
return []
}
