0

Can you tell me how to wait till one subscription is being finished?

Note: I need to wait till this let survey = this.getSurveys(currentQuestionCode) is being finished.After that this return this.question = value; should be executed. Here items are getting from the JSON array.

Update: My question is different than the mentioned duplicate issue.The question here is how to wait till 2nd observable has finished its work.

page.ts

 getQuestions(currentQuestionCode: string): any {
    this.surveyData.getQuestions().subscribe(
      result => {

        _.forEach(result, (value, key) => {

          if (key == currentQuestionCode) {
            let survey = this.getSurveys(currentQuestionCode);//here is the issue
            return this.question = value;
          }
        });
      },
      err => { },
      () => { }
    );
  }

  getSurveys(currentQuestionCode: string): any {
    this.surveyData.getSurveys().subscribe(
      result => {

        _.forEach(result, (value, key) => {

          if (key == currentQuestionCode) {
            return this.survey = value;
          }
        });
      },
      err => { },
      () => { }
    );
  }

Service.ts

 //get Questions
  getQuestions(): Observable<any> {
    return this.http.get("/assets/data/questions.json")
      .map(this.extractData)
      .catch(this.handleError);

  }

  //get Surveys
  getSurveys(): Observable<any> {
    return this.http.get("/assets/data/survey.json")
      .map(this.extractData)
      .catch(this.handleError);

  }
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • 1
    Possible duplicate of [Angular 2 - Return data directly from an Observable](http://stackoverflow.com/questions/37867020/angular-2-return-data-directly-from-an-observable) – jonrsharpe May 01 '17 at 15:47
  • @jonrsharpe That is not my issue.Can you please remove the duplicate? Please allow others to provide an answer. I need a help here. – Sampath May 01 '17 at 15:57
  • 1
    *"My question is different"* - **how**, specifically? Don't just assert, *explain*. You seem to want to return the data; you can't do that, as the other question says. If you actually returned the *observable* from `getSurveys`, you could `.subscribe` to it in `getQuestions`, or `.map`/`.flatMap` some transformation and so on up to whatever is consuming the data. – jonrsharpe May 01 '17 at 16:01
  • 1
    Are you sure you do? Because your code doesn't actually return anything from `getSurveys`. The `return` in the inner callback will allow `forEach` to create an array, but *that doesn't go anywhere*. Instead you probably need to, again, *return an observable* from `getSurveys`, so that `getQuestions` can consume the result. I'd recommend reading up on RxJS (see also e.g. http://stackoverflow.com/q/37304160/3001761, there's plenty of information out there), and maybe being a little less rude. – jonrsharpe May 01 '17 at 16:08
  • Also, what is the purpose of this code? To find the survey response for one survey question? It seems inefficient to get all of the survey questions and loop through them to find the one you want. It would be must better to request from the server the *one* survey question that you want. – DeborahK May 01 '17 at 16:45
  • Here it is getting from the JSON array. We don't have any method other than the loop through all items. @DeborahK – Sampath May 01 '17 at 16:48
  • @Sampath I would rather suggest you to take the complete `questions` and then raise a request for `surveys`. if you want to wait for that that particular request to complete you will go chaining and same request will be triggered either twice or thrice depending on the delay in data – Aravind May 01 '17 at 17:09
  • Can you put that as an answer? Then I can try that. Thanks @Aravind – Sampath May 01 '17 at 17:10
  • can you say how the services look like in both of these case?. I said in the previous comment is because I had a similar situation. – Aravind May 01 '17 at 17:13
  • I have updated the question.Please see that @Aravind – Sampath May 01 '17 at 17:15
  • This seems like a case where `Observable.forkJoin` would make more sense. – Dylan May 01 '17 at 18:46

1 Answers1

0

Rxjs has Observable methods to handle this.

getBoth(){
   return getQuestions()
      .combineLatest(getSurveys())
      .map(([q, s]) => {
         return `${q} ${s}`;
      });
}
...

getBoth().subscribe(...);
Dylan
  • 4,703
  • 1
  • 20
  • 23