Aquí tenemos las clases GetDataAsyncService que espera el cambio en la tienda (y no ejecuta el bloque de código debajo de él hasta un cambio en la tienda ( this.getDataAsyncService.getAsyncData().subscribe((data)=>{ )} ). Cuando se llama desde MainComponent , obtendrá el retorno de (propA); (de GetDataAsyncService) antes de que se ejecute el bloque de código en el oyente, porque el oyente todavía está esperando un cambio en la tienda. Quiero emitir solo ese observable cuando se ejecuta ese bloque de operación.
export class GetDataAsyncService { propA; constructor(private store: Store<AppState>) getData():Observable<any>{ this.store.pipe(select(appState)).subscribe((val)=>{ // operation block // some operations // some more operations this.propA = val.propA; }) return of(propA); // this should be emitted with the latest value only when the block of code above executes - not before that } } export MainComponent implenents OnInit{ propA: string = ''; constructor(private getDataAsyncService: GetDataAsyncService){} ngOnInit(): void{ this.getDataAsyncService.getAsyncData().subscribe((data)=>{ this.propA = data.propA; }) } // any operation involving propA // code ...... }Puede lograrlo devolviendo el Observable mismo desde la función getData y asignándolo al accesorio requerido, en lugar de subscribe a él, como se muestra a continuación:
export class GetDataAsyncService { propA; constructor(private store: Store<AppState>) {} getData(): Observable<any> { return this.store.pipe( select(appState), map((val) => val.propA) ); } } export class MainComponent implements OnInit { propA: string = ''; constructor(private getDataAsyncService: GetDataAsyncService) {} ngOnInit(): void { this.getDataAsyncService.getAsyncData().subscribe((propA) => { this.propA = propA; }); } }