简体   繁体   中英

How to convert a function that returns an observable to an arrow function?

I have the following code which works fine. What I want is to convert the getSaveObs function to an arrow function so I can use it inside the save function.

private save(property: IProperty): void {
  // I want the getSaveObs logic to be moved here instead of a separate function
  this.getSaveObs(property)
   .subscribe(savedProperty => {
    this.property = savedProperty;
    this.propertyStorageService.storeProperty(this.property);
    this.propertyDetailsForm.get('id').setValue(this.property.id);
    // if new property, move to step 2
    if (!savedProperty.id) {
      console.log('New property, moving to step 2');
      if (this.clientId) {
        this.router.navigateByUrl('/clients/' + this.clientId + '/properties/' + this.property.id + '/' + '2');
      } else {
        this.router.navigate(['/properties', this.property.id, '2']);
      }
    }
  }, error => (console.error('Error adding/updating a property: ', error)));
}


private getSaveObs(property: IProperty): Observable<IProperty> {
  if (property.id) {
    if (this.clientId) {
        return this.clientPropertiesService.updateProperty(this.accountService.userIdentity.userId,                
           this.clientId, property);
    } else {
        return this.profileService.updateProperty(this.accountService.userIdentity.userId, property);
    }
  } else {
    // New property observable
    if (this.clientId) {
        return this.clientPropertiesService.createProperty(this.accountService.userIdentity.userId, 
           this.clientId, property);
    } else {
      return this.profileService.createProperty(this.accountService.userIdentity.userId, property);
    }
  }
}

Here is one of the methods called that returns Observable

createUserProperty(property: IProperty, userDocument: 
    AngularFirestoreCollection): EntityResponseType {
    delete property.id;
    property.modifiedTs = firebase.firestore.Timestamp.now();
    property.createdTs = firebase.firestore.Timestamp.now();

    return from(userDocument.add(property))
      .pipe(
         map(res => {
         return {...property, id: res.id};
      })
    );
 }

Why do you believe that an arrow function can be used in save(property: IProperty): void and a regular function cannot? What are you trying to do?


Regardless any function can be turned into an arrow function like this

function someName(arg: ArgType): ReturnType {
  /*...function body...*/
}

becomes

const someName = (arg: ArgType): ReturnType => {
  /*...function body...*/
}

SO in your case, that's:

const getSaveObs = (property: IProperty): Observable<IProperty> => {
  if (property.id) {
    if (this.clientId) {
        return this.clientPropertiesService.updateProperty(this.accountService.userIdentity.userId,                
           this.clientId, property);
    } else {
        return this.profileService.updateProperty(this.accountService.userIdentity.userId, property);
    }
  } else {
    // New property observable
    if (this.clientId) {
        return this.clientPropertiesService.createProperty(this.accountService.userIdentity.userId, 
           this.clientId, property);
    } else {
      return this.profileService.createProperty(this.accountService.userIdentity.userId, property);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM