Stop the execution of an Observable chain based on a condition











up vote
1
down vote

favorite












In an Angular application, I have this Observable chain in my component :



@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {


search.component.ts



results: any = ;
queryField: FormControl = new FormControl();
constructor(private _apiService: ApiService) { }

ngOnInit() {

this.queryField.valueChanges
.debounceTime(200)
.distinctUntilChanged()
.switchMap((query) => this._apiService.search(query)) // was subscribe
.subscribe(result => { this.results = result.items; console.log(result); });


}
}



seach.ccomponent.html



<input [formControl]='queryField' type="text" id="keyword" autocomplete="off" placeholder="start typing..."/>


What I would like to do is to cancel the execution of the rest of the Observables chain and don't go to the switchMap operator (in order to don't execute the request) if the value emitted du the formControl is null.










share|improve this question




























    up vote
    1
    down vote

    favorite












    In an Angular application, I have this Observable chain in my component :



    @Component({
    selector: 'app-search',
    templateUrl: './search.component.html',
    styleUrls: ['./search.component.css']
    })
    export class SearchComponent implements OnInit {


    search.component.ts



    results: any = ;
    queryField: FormControl = new FormControl();
    constructor(private _apiService: ApiService) { }

    ngOnInit() {

    this.queryField.valueChanges
    .debounceTime(200)
    .distinctUntilChanged()
    .switchMap((query) => this._apiService.search(query)) // was subscribe
    .subscribe(result => { this.results = result.items; console.log(result); });


    }
    }



    seach.ccomponent.html



    <input [formControl]='queryField' type="text" id="keyword" autocomplete="off" placeholder="start typing..."/>


    What I would like to do is to cancel the execution of the rest of the Observables chain and don't go to the switchMap operator (in order to don't execute the request) if the value emitted du the formControl is null.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In an Angular application, I have this Observable chain in my component :



      @Component({
      selector: 'app-search',
      templateUrl: './search.component.html',
      styleUrls: ['./search.component.css']
      })
      export class SearchComponent implements OnInit {


      search.component.ts



      results: any = ;
      queryField: FormControl = new FormControl();
      constructor(private _apiService: ApiService) { }

      ngOnInit() {

      this.queryField.valueChanges
      .debounceTime(200)
      .distinctUntilChanged()
      .switchMap((query) => this._apiService.search(query)) // was subscribe
      .subscribe(result => { this.results = result.items; console.log(result); });


      }
      }



      seach.ccomponent.html



      <input [formControl]='queryField' type="text" id="keyword" autocomplete="off" placeholder="start typing..."/>


      What I would like to do is to cancel the execution of the rest of the Observables chain and don't go to the switchMap operator (in order to don't execute the request) if the value emitted du the formControl is null.










      share|improve this question















      In an Angular application, I have this Observable chain in my component :



      @Component({
      selector: 'app-search',
      templateUrl: './search.component.html',
      styleUrls: ['./search.component.css']
      })
      export class SearchComponent implements OnInit {


      search.component.ts



      results: any = ;
      queryField: FormControl = new FormControl();
      constructor(private _apiService: ApiService) { }

      ngOnInit() {

      this.queryField.valueChanges
      .debounceTime(200)
      .distinctUntilChanged()
      .switchMap((query) => this._apiService.search(query)) // was subscribe
      .subscribe(result => { this.results = result.items; console.log(result); });


      }
      }



      seach.ccomponent.html



      <input [formControl]='queryField' type="text" id="keyword" autocomplete="off" placeholder="start typing..."/>


      What I would like to do is to cancel the execution of the rest of the Observables chain and don't go to the switchMap operator (in order to don't execute the request) if the value emitted du the formControl is null.







      angular rxjs observable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 4 '17 at 9:52

























      asked Feb 3 '17 at 22:08









      Nacim Idjakirene

      50941127




      50941127
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          So if I understand correctly you want to .filter() emissions if they are null so your switchMap is not executed:



          this.queryField.valueChanges
          .filter((q) => q !== null) // only proceed if the value is not null
          .debounceTime(200)
          .distinctUntilChanged()
          .switchMap((query) => this._apiService.search(query)) // was subscribe
          .subscribe(result => {
          this.results = result.items;
          console.log(result);
          });





          share|improve this answer



















          • 3




            I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
            – Matt
            Jun 1 at 13:07










          • Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
            – Mark van Straten
            Jun 4 at 18:45










          • Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
            – Matt
            Jun 4 at 19:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42033789%2fstop-the-execution-of-an-observable-chain-based-on-a-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          So if I understand correctly you want to .filter() emissions if they are null so your switchMap is not executed:



          this.queryField.valueChanges
          .filter((q) => q !== null) // only proceed if the value is not null
          .debounceTime(200)
          .distinctUntilChanged()
          .switchMap((query) => this._apiService.search(query)) // was subscribe
          .subscribe(result => {
          this.results = result.items;
          console.log(result);
          });





          share|improve this answer



















          • 3




            I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
            – Matt
            Jun 1 at 13:07










          • Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
            – Mark van Straten
            Jun 4 at 18:45










          • Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
            – Matt
            Jun 4 at 19:28















          up vote
          5
          down vote













          So if I understand correctly you want to .filter() emissions if they are null so your switchMap is not executed:



          this.queryField.valueChanges
          .filter((q) => q !== null) // only proceed if the value is not null
          .debounceTime(200)
          .distinctUntilChanged()
          .switchMap((query) => this._apiService.search(query)) // was subscribe
          .subscribe(result => {
          this.results = result.items;
          console.log(result);
          });





          share|improve this answer



















          • 3




            I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
            – Matt
            Jun 1 at 13:07










          • Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
            – Mark van Straten
            Jun 4 at 18:45










          • Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
            – Matt
            Jun 4 at 19:28













          up vote
          5
          down vote










          up vote
          5
          down vote









          So if I understand correctly you want to .filter() emissions if they are null so your switchMap is not executed:



          this.queryField.valueChanges
          .filter((q) => q !== null) // only proceed if the value is not null
          .debounceTime(200)
          .distinctUntilChanged()
          .switchMap((query) => this._apiService.search(query)) // was subscribe
          .subscribe(result => {
          this.results = result.items;
          console.log(result);
          });





          share|improve this answer














          So if I understand correctly you want to .filter() emissions if they are null so your switchMap is not executed:



          this.queryField.valueChanges
          .filter((q) => q !== null) // only proceed if the value is not null
          .debounceTime(200)
          .distinctUntilChanged()
          .switchMap((query) => this._apiService.search(query)) // was subscribe
          .subscribe(result => {
          this.results = result.items;
          console.log(result);
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 23:47









          Joshua Rowlison

          1295




          1295










          answered Feb 3 '17 at 22:24









          Mark van Straten

          6,52222349




          6,52222349








          • 3




            I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
            – Matt
            Jun 1 at 13:07










          • Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
            – Mark van Straten
            Jun 4 at 18:45










          • Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
            – Matt
            Jun 4 at 19:28














          • 3




            I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
            – Matt
            Jun 1 at 13:07










          • Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
            – Mark van Straten
            Jun 4 at 18:45










          • Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
            – Matt
            Jun 4 at 19:28








          3




          3




          I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
          – Matt
          Jun 1 at 13:07




          I'm puzzled. You've got over 5000 reputation points and post a comment as an answer?!
          – Matt
          Jun 1 at 13:07












          Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
          – Mark van Straten
          Jun 4 at 18:45




          Hmm i could have rephrased this answer to feel less like a comment. Nevertheless the answer i gave is correct given the question asked.
          – Mark van Straten
          Jun 4 at 18:45












          Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
          – Matt
          Jun 4 at 19:28




          Hey Mark, I was just referring to your first version without the code snippet. Nothing wrong with the current one.
          – Matt
          Jun 4 at 19:28


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42033789%2fstop-the-execution-of-an-observable-chain-based-on-a-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen