This is more of a syntax question rather than a bug I'm facing.
The process is straightforward:
- Send an HTTP request that returns a boolean value
- If the boolean is true, proceed
- If the boolean is false, log a warning and stop the flow.
To handle this, my current code looks like this:
Basic Setup
private _getBoolean() { return this.http.get(...); }
private _getData() { return this.http.get(...); }
Current Implementation
public getData() {
return this._getBoolean().pipe(
filter(bool => {
if(!bool) {
console.warn('Incorrect server response, stream stopped');
return false;
}
return true;
}),
switchMap(bool => this._getData())
);
}
However, I feel like there might be a more natural and optimized way to do this.
I was wondering if there is a simpler syntax, something like this:
public getData() {
return this._getBoolean().pipe(
throwError(bool => bool ? new Error('Incorrect server response, stream stopped') : null),
catchError(err => console.warn(err)),
switchMap(bool => this._getData())
);
}
Is there an approach similar to this, or am I using the correct syntax?