Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning rxjs function of type Observable<void>

Tags:

rxjs

I'm trying to convert the below observable code from using Observable.create to using pipeable operators.

public getUnrecoveredGearsAfterDep(): Observable<void> {

    return Observable.create((observer) => {      

      this.vesselDetailService.getVessel().subscribe(
        (vessel: Vessel) => {          
          console.log(vessel.cfr);
          this.getAndUpdateUnrecoveredGears(vessel.cfr).subscribe(
            () => {              
              observer.next(null);
              observer.complete();
            },
            (error) => {
              observer.error(error);
            }
          );
        },
        (error) => {
          observer.error(error);          
        }
      );    

    });
  }

I've tried the below method but I'm getting an conversion error cant convert type Observable.unknown to Observable.void any help would be appreciated.

public altGetUnrecoveredGearsAfterDep(): Observable<void> {

    return this.vesselDetailService.getVessel().pipe(
      tap( (vessel: Vessel) => console.log(vessel.cfr)),
      switchMap((vessel: Vessel) => this.getAndUpdateUnrecoveredGears(vessel.cfr)),
      map((gears: GearSet[]) => of())      
    );
  }
like image 459
declan roche Avatar asked Jun 25 '26 03:06

declan roche


1 Answers

Your problem is that you are mapping your output to of() which is an observable. This gives your function a return type of Observable<Observable<never>>.

Your map should simply return undefined or null, you don't need of:

public altGetUnrecoveredGearsAfterDep(): Observable<void> {

    return this.vesselDetailService.getVessel().pipe(
      tap( (vessel: Vessel) => console.log(vessel.cfr)),
      switchMap((vessel: Vessel) => this.getAndUpdateUnrecoveredGears(vessel.cfr)),
      map((gears: GearSet[]) => undefined)      
    );
  }

Also, you could simplify by using mapTo and removing the type annotations:

public altGetUnrecoveredGearsAfterDep(): Observable<void> {

    return this.vesselDetailService.getVessel().pipe(
      tap(vessel => console.log(vessel.cfr)),
      switchMap(vessel => this.getAndUpdateUnrecoveredGears(vessel.cfr)),
      mapTo(undefined)      
    );
  }
like image 118
BizzyBob Avatar answered Jun 29 '26 09:06

BizzyBob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!