HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Return an empty Observable

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
returnobservableempty

Problem

The function more() is supposed to return an Observable from a get request
export 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

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.