1
_subscriptions.push(
  // Update list on OG selection
  ogViewModel.current$.subscribe(
    (group) => {
      this.groupUuid = group.ogUuid;
      this.groupId = group.id;
      this.getDevices();
      this.getSubOgs();
      this.updateGroup(group);
    },
  ),

I have a block of code inside subscribe. However, it seems that they are not executed in order. this.getSubOgs() is executed before this.getDevices(). Both are HTTP calls that returns an observable. How do I make sure this.getDevices() is executed first? Click to See Codes

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    You can try putting this.getSubOgs() at the end of this.getDevices() method – Misha Mashina Mar 03 '22 at 15:40
  • use async await to run them synchronously – Piyush Jain Mar 03 '22 at 15:46
  • @PiyushJain How to use async await here? I am new to angular and subcription. – Cindy Zhang Mar 03 '22 at 16:03
  • @MishaMashina I can't put this.getSubOgs() at the end of this.getDevices() method because they are two separate methods that need to be referenced in other places. – Cindy Zhang Mar 03 '22 at 16:04
  • @AndrewCorrigan Unfortunately I can't provide more codes since they are industry codes. However, in both getDevices() and getSubOgs(), we call the method that makes an HTTP call and return the result as an observable. – Cindy Zhang Mar 03 '22 at 16:08
  • @CindyZhang - you could've at least posted the content of that screen shot as code; makes it more readable. – Andrew Corrigan Mar 03 '22 at 16:14
  • As an aside: this should help https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Andrew Corrigan Mar 03 '22 at 16:17
  • @AndrewCorrigan You can click on the link in the post, it will show you the screenshot of the codes. – Cindy Zhang Mar 03 '22 at 16:31
  • https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557 – Andrew Corrigan Mar 03 '22 at 16:35
  • I'm confused. Why is the observable even pushed into an array? What's the goal of this? But if you want to use several observables in succession, you might want to look into mergeMap() in RxJs – ak.leimrey Mar 04 '22 at 17:26
  • use [concat](https://www.learnrxjs.io/learn-rxjs/operators/combination/concat) rxjs/operator – Eliseo Mar 08 '22 at 15:28

1 Answers1

0

this is basically @MishaMashina's suggestion

    _subscriptions.push(ogViewModel.current$.subscribe(
    (group) => {
      this.groupUuid = group.ogUuid;
      this.groupId = group.id;
      this.getDevices();
      this.updateGroup(group);
    },
  ),

both getDevices() and getSubOgs() remain seperate methods

in

public getDevices() { this.myApiClient.getDevices().Subscribe(
let onlyThisTime: boolean = true;
    (next) => doStuff
    (error) => doErrorStuff
    () => {if(onlyThisTime){this.getSubOgs();}}
    )}
qqtf
  • 632
  • 6
  • 17