How to make a service luanch null or 0 if a method param is false

There are multiple solutions to it.

Option 1: RxJS of function

You could return null or false using RxJS of function.

import { of } from 'rxjs';

ChangeACCStatus(IsConfigurated): Observable<any> {
  if (!!IsConfigurated) {
    const dataStruct = { SessionId: localStorage.getItem('SessionId') };
    return this.http.post(`${this.CommandeURL1}/TryToChangeAccAlarm`, JSON.stringify(dataStruct), this.httpOptions);
  }
  return of(null); // <-- or `of(false)`
}

Note that this will trigger the next callback of the subscription as if it’s a valid response. So you need to handle it.

Option 2: RxJS EMPTY constant

You could return RxJS EMPTY constant that will trigger the complete callback of the subscription and complete.

import { EMPTY } from 'rxjs';

ChangeACCStatus(IsConfigurated): Observable<any> {
  if (!!IsConfigurated) {
    const dataStruct = { SessionId: localStorage.getItem('SessionId') };
    return this.http.post(`${this.CommandeURL1}/TryToChangeAccAlarm`, JSON.stringify(dataStruct), this.httpOptions);
  }
  return EMPTY;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top