1

The component has variable applicationObs as observer:

export class OrderDetailsComponent implements OnInit {
    public applicationObs: Observable<ApplicationResponse>;
    
     public getSections(): SidebarMenuItem[] {
        const fabricApplicationSidebar = fabricApplicationSidebarMenu(this.application); // Here 
     }
}

How to pass applicationObs to fabricApplicationSidebarMenu() when this.application is not observer, but ApplicationResponse.

How to pass async data as variable in function?

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Obserables are async operations. To unwrap an observer you need to subscribe to it. Have read of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Aug 03 '20 at 12:27
  • Does this answer your question? [RxJs get value from observable](https://stackoverflow.com/questions/37607257/rxjs-get-value-from-observable) – Liam Aug 03 '20 at 12:29
  • @Liam Observables are not always async. Here a nice Post about this: https://christianlydemann.com/are-observables-async/ – enno.void Aug 03 '20 at 12:32
  • That article appears to be mixing running order and asynchousity up. An asynchronous task can run immediately. That doesn't make it synchronous. It also has no bearing what-so-ever on this question – Liam Aug 03 '20 at 12:39

2 Answers2

1

I'd suggest you think about this a little differently, what you actually want to do is add a new function onto your observable pipe. so I'd do something like this:

export class OrderDetailsComponent implements OnInit {
    public applicationObs: Observable<ApplicationResponse>;
    
     public getSections(): Observable<SidebarMenuItem[]> {
        return applicationObs.pipe(
          map(m => fabricApplicationSidebarMenu(m))
        );

     }
}

Here were adding a new pipe onto the existing observable and returning that. this is still async so in itself doesn't do anything. To get the sections you'll still need to subscribe to the resulting observable:

this.getSections().subscribe((s: SidebarMenuItem[]) => {

});

You could also potentially use an async pipe if you wished. See here

Note that the above will trigger the applicationObs and whatever fabricApplicationSidebarMenu does on subscribe. This may or may not be desirable depending on what your planning on doing and what these functions do.

Liam
  • 27,717
  • 28
  • 128
  • 190
0

what do you want to do? something like this?

this.applicationObs.subscribe(
(applicationResponse: ApplicationResponse) => {
   const fabricApplicationSidebar = fabricApplicationSidebarMenu(applicationResponse); // Here 
});
gaetan224
  • 586
  • 3
  • 13