patternjavascriptModerateCanonical
Return an empty Observable
Viewed 0 times
observableemptyreturn
Problem
The function
In this case I can only do a request if
more() is supposed to return an Observable from a get requestexport class Collection {
public more = (): Observable => {
if (this.hasMore()) {
return this.fetch();
} else {
// return empty observable
}
};
private fetch = (): Observable => {
return this.http.get("some-url").map((res) => {
return res.json();
});
};
}
In this case I can only do a request if
hasMore() is true, else I get an error on subscribe() function subscribe is not defined, how can I return an empty Observable?this.collection.more().subscribe(
(res) => {
console.log(res);
}, (err) => {
console.log(err);
}
);
Solution
Since all the answers are outdated, I will post the up to date answer here
In RXJS >= 6
In RXJS >= 6
import { EMPTY } from 'rxjs'
return EMPTY;Code Snippets
import { EMPTY } from 'rxjs'
return EMPTY;Context
Stack Overflow Q#38548407, score: 37
Revisions (0)
No revisions yet.