1

I know there have been several questions regarding this, but I havent found a way that works for me properly. If you find one gladly send it to me.

So I am getting my data from an API and I convert this data into an array of my required datatype:

getData(): Observable<Type[]> {
    return this.http.get<Type[]>("URL)
      .pipe(
        retry(2),
        catchError(this.handleError)
      );
  }

I want to use the data, after I got it.

Some ways i tried to solve this were with while(empty){...} which seems to be unefficient and not correct if the array is empty, Promises where I didn't really find a way to connect properly with observables and await/async which seems to be impossible with observables.

My code of implementing this:

array: Type[] = [];
useJSON(searchString: string) {
    this.configService.getData().subscribe(results => { this.array = results});
    // --operations--
  }
5
  • So you want to listen as long as some particular array will come ? await/async can be used with promises (and observables can be promises ) Commented Jul 24, 2020 at 8:06
  • 1
    It is not impossible to use async/await with observables. If you are sure it is emitted just once you can always convert it to a promise. But if you want it to keep the observable way, you need to do your "further operations" in your pipe. Commented Jul 24, 2020 at 8:09
  • What do you mean by I do need to perform some additional operations to use this data and I need to be sure that this array is already filled up with the whole data. ? Received data is immutable - it wont change over time - you have to make new request. My answer is for what Iv understand that you want to fetch some data over and over again until it meets some condition. Commented Jul 24, 2020 at 8:15
  • Ok, so I want to perform my operations which use the data, after I got it. Commented Jul 24, 2020 at 8:22
  • 1
    @bena: Do it inside the subscription. That's the correct way to do something over async variables obtained from observables. Commented Jul 24, 2020 at 8:23

2 Answers 2

1

async/away should work for you (syntax might be slightly invalid)

youdMethod() async{
let res=null;

do{
 res=await this.configService.getData().toPromise();
}while(resBasedCondition)
 
}

what you probably wat is to have operator repeatIf but it is not available. Behavior can be mimic using expand as shown here https://stackoverflow.com/a/51644077/1527544

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

Comments

1

I am not exactly sure what you're looking for. If you need to do further operations after the this.array is initialized, then include them inside the subscription after the assignment.

array: Type[] = [];
useJSON(searchString: string) {
  this.configService.getData().subscribe(results => { 
    this.array = results;
    // --further operations--
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.