11

I'm trying to subscribe to an Observable and assign some data from the response, but somehow my code it's not waiting for the response. Basically the console.log(this.newIds) is runned first and is always empty because the subscribe doesn't wait for response to come from the backend. How I can force my code to wait for the response to come?

 this.repository.getById(Ids).subscribe((response) => {
      console.log(response);
      this.newIds = response.map((id) => {
        return id;
      });
    });
    console.log(this.newIds);
0

5 Answers 5

8

If you put the code in the subscribe callback. It will execute after your receive a response from the back-end. All code you write outside this function is directly execute.

     this.repository.getById(Ids).subscribe((response) => {
          //Code will execute when back-end will respond
          console.log(response);
          this.newIds = response.map((id) => {
            return id;
          });
          console.log(this.newIds);
        });
//Code will execute immediately

See also : https://angular.io/guide/observables#creating-observables

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! One more question. Actually I want to use the this.newIds variable, not just to log it, so I still need to wait for the response to come, otherwise the variable will still be empty (I dont want to use it inside the subscribe). Should I do Promise instead of Observable?
No, I think Promise is outdated for Angular. I guess, you have to setup a ngrx store to manage data that use in multiple component. If you haven't time to setup this, maybe just use @Input. I think it's a another question, If you could put a new question for helping others users, and validate an answer for the first one. Thank you :).
but still, he can transform the observable to a promise for this particular case using .toPromise()
@MohamadAlAsmar Yes you right. It's possible. I think it's more easy to keep a clean app when you only use Observables and not Observable + Promises + etc...
5

This is the normal behaviour because your console.log(this.newIds); is outside of the subscription, you just need to move it inside the .subscribe() method:

 this.repository.getById(Ids).subscribe((response) => {
          console.log(response);
          this.newIds = response.map((id) => {
            return id;
          });
          console.log(this.newIds);
    });

if you want to use this.newIds outside the subscription and immediately after the result of the observer you can use RxJs .toPromise() to use it as a promise and change the method to async:

async callerFn(){

    const response = await this.repository.getById(Ids).toPromise();
  
    this.newIds = response.map((id) => {
        return id;
    });
    console.log(this.newIds);
    // use your property here

 }

1 Comment

Thank you! One more question. Actually I want to use the this.newIds variable, not just to log it, so I still need to wait for the response to come, otherwise the variable will still be empty (I dont want to use it inside the subscribe). Should I do Promise instead of Observable?
2

Yes, Because the Javascript is interpreting line-by-line execution so that it will not wait for other processes to complete. That's why the last console will return undefined. In the same time if you use the console inside of the subscriber then you will get proper log because the subscriber will wait for the response and bind it with this.newIds

 this.repository.getById(Ids).subscribe((response) => {
      console.log(response);
      this.newIds = response.map((id) => {
        return id;
      });
     console.log(this.newIds);
    });

Here I'm attaching a good read about the observable subscribe

https://betterprogramming.pub/observables-vs-promises-which-one-should-you-use-c19aef53c680

In addition to this, If you want to go with the newIds access outside of subscriber scope please use promise with async await. Here I'm adding a sample

 async getAsyncData() {
    this.asyncResult = await this.httpClient.get<Employee>(this.url).toPromise();
    console.log('No issues, I will wait until promise is resolved..');
  }

2 Comments

Thank you! One more question. Actually I want to use the this.newIds variable, not just to log it, so I still need to wait for the response to come, otherwise the variable will still be empty (I dont want to use it inside the subscribe). Should I do Promise instead of Observable?
Yes, you can use promise with async await, I will update the answer with one sample too
1

You can do like this..

your component file like below

newIds: Observable<any> = of(this.id).pipe(
concatMap((id) =>
  this.getId(id).pipe(map((data) => data.map((rowId) => rowId.id)))
)
);
getId(id: any) {
  return of([{ id: 1 }, { id: 2 }, { id: 3 }]);
}

your html file like below, and use async pipe for subscription. here you can use concateMap pipe rxjs operator to sequentially calling observable and then assing value to your newId varible.

<pre>
  {{ newIds | async }}
</pre>

Demo in this live link Stackblitz Link

Comments

0

I would approach in a different way: if you have to remap the value you can use map operator:


 this.repository.getById(Ids)
  .pipe(map(response) => response.map(id => id))
  .subscribe((id) => {
      console.log(response);
      this.newIds = id;
  });

Actually, I don't understand why you need to map a value that you already have, but I think this is the clear solution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.