I have two services: one is dependent on the other. Service A makes makes an http call to get data. Service B actually uses that data.
Service A:
@Injectable({
providedIn: 'root'
})
export class ServiceA {
data: MyData;
getData(): Observable<MyData> {
return this.http.get<Mydata>('http://some.url')
.pipe(
tap((data: MyData) => {console.log(`got data');})
)
);
};
}
Service B:
@Injectable({
providedIn: 'root'
})
export class ServiceB {
obs = Observable<MyData> = new Observable<MyData>();
processedData: string[];
constructor(private serviceA: ServiceA) {
this.obs = this.serviceA.getData();
this.obs.subscribe(
data => {this.processedData = process(data)},
error => { /*process error*/ },
function() { /* maybe mark flag here? */}
);
}
process(endpointData) {
// Do some business logic to endpointData
this.processedData = endpointData;
}
processedData() {
// The first time this is called, the observable hasn't completed
}
}
A client of Service B will call processedData(). Just curious how to elegantly wait on the observable within processData(). The non-async side of me would want to check if the finally part of the observable has been called. If so, just use this.processedData. If not... then what? I suppose I could only subscribe the one time, within processedData, and only on the first call. That still seems not so correct. Thoughts?