Although there have been numerous discussions on this topic, I am still struggling to find a solution that works well for me. If you come across one, please do share it with me.
Currently, I am fetching data from an API and converting it into an array of the required datatype:
getData(): Observable<Type[]> {
return this.http.get<Type[]>("URL)
.pipe(
retry(2),
catchError(this.handleError)
);
}
Once I receive the data, I want to use it in my application.
I've tried utilizing while(empty){...}
, which appears to be inefficient and incorrect if the array is empty. I also experimented with Promises
, but couldn't figure out how to seamlessly integrate them with observables. Additionally, I explored using await/async
, but it seems incompatible with observables.
This is how I have implemented the code:
array: Type[] = [];
useJSON(searchString: string) {
this.configService.getData().subscribe(results => { this.array = results});
// --operations--
}