0

I am new to Angular (used AngularJs for a number of years) and I am struggling with Observables :( I have this method:

filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
  var formulas = this.formulaService.getFromSelectedAnswers(questions);
  let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
  if (!shouldFilter) return;

  return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
    if (!products || !products.length) return;

    this.products.length = 0;
    this.products.push.apply(this.products, products);
    this.questionService.filterQuestionsByProducts(products, questions);      
    this.questionService.updateSession(questions);
  }));
}

The line if (!shouldFilter) return; is not right. I need it to return an Observable so my subscriptions work. Does anyone know how to do that?

2

1 Answer 1

2

You can return empty, if you don't care about return values in subscription. which will complete the observable.

import {empty} from 'rxjs';

filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
  var formulas = this.formulaService.getFromSelectedAnswers(questions);
  let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
  if (!shouldFilter) return empty();

  return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
    if (!products || !products.length) return empty();

    this.products.length = 0;
    this.products.push.apply(this.products, products);
    this.questionService.filterQuestionsByProducts(products, questions);      
    this.questionService.updateSession(questions);
  }));
Sign up to request clarification or add additional context in comments.

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.