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.
angular rxjs observable
add a comment |
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.
angular rxjs observable
add a comment |
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.
angular rxjs observable
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
angular rxjs observable
edited Feb 4 '17 at 9:52
asked Feb 3 '17 at 22:08
Nacim Idjakirene
50941127
50941127
add a comment |
add a comment |
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);
});
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
add a comment |
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);
});
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
add a comment |
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);
});
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
add a comment |
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);
});
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);
});
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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