从订阅打字稿返回值

Returning value from subscription typescript

我如何 return 通过 Save() 方法取得成功。

public SaveItem() {
 if(save()){ // The goal is to use save method like this
  // Close pop up;
}

public SaveAndNew() {
 if(save()){  // The goal is to use save method like this
  // Create new item;
}


private save() {
 let issuccess = false;

 this.myservice.AddParty(newUserObject)
  .subscribe(data => {   
    if (data['status'].toString() === '1') {
      return issuccess = false;
    } else {
      return issuccess = true;
    }
  },
    (er) => {

      return issuccess = false;
    });
}

我将如何等待保存功能和 return 基于响应的特定值?

我读过有关回调的内容,它似乎并不优雅,有什么优雅的方法可以做到这一点

callbacks-vs-promises-vs-rxjs-vs-async-awaits

如果是 C# 我会这样做

var isSuccess = await SaveAsync(party);

In this case you can use promise instead of subscribe. But while binding the data into html, then you should go with async pipe

所以,你的服务会像

 AddParty(newUserObject) {
        return this.http.post(url)
            .toPromise().then(responce => <any[]>responce.json())
            .catch(error => {
                return error;
            });
    }

并检索看起来像

this.myservice.AddParty(newUserObject)
            .then(data => {
                if (data['status'].toString() === '1') {
                    return issuccess = false;
                } else {
                    return issuccess = true;
                }
            },
            (er) => {

                return issuccess = false;
            });
    }

这样的事情怎么样:

public SaveItem() {
 const isNew = false;
 save(isNew)
}

public SaveAndNew() {
 const isNew = true;
 save(isNew)
}


private save(isNew) {

 this.myservice.AddParty(newUserObject)
  .subscribe(data => {   
    if (data['status'].toString() === '1') {
      saveComplete(isNew);
    } else {
      saveFailed(isNew)
    }
  },
    (er) => {
      saveFailed(isNew)
    });
}

saveComplete(isNew) {
  // Check isNew as needed.
  // Close dialog or whatever
}

saveFailed(isNew) {
  // do whatever here
}

或者 ... TypeScript 现在支持 async/await ... 所以你可以考虑在这里使用它们。有关更多信息,请参阅:https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875

你可以让你的保存方法return成为布尔值的可观察值

public SaveAndNew() {

this.save().subscribe(success =>
{
    if(success)
    {
      // Create new item;
    }
});

private save() : Observable<boolean> {

 return this.myservice
            .AddParty(newUserObject)
            .map(data=> data['status'].toString() === '1')
            .catch(err => Observable.of(false)); 
}

试一试

public SaveItem() {
 if(save()){ // The goal is to use save method like this
  // Close pop up;
}

public SaveAndNew() {
 if(save()){  // The goal is to use save method like this
  // Create new item;
}


private async save() {
 let issuccess = false;

await  this.myservice.AddParty(newUserObject)
  .subscribe(data => {   
    if (data['status'].toString() === '1') {
      return issuccess = false;
    } else {
      return issuccess = true;
    }
  },
    (er) => {

      return issuccess = false;
    });
      return issuccess ;
}

我们在 subscribe() 中有另一个操作参数,可以帮助您在不使用 observable 的情况下 return。

  public SaveItem() {
     if(save()){ // The goal is to use save method like this
      // Close pop up;
    }

    public SaveAndNew() {
     if(save()){  // The goal is to use save method like this
      // Create new item;
    }


    private save() {
     let issuccess = false;

     this.myservice.AddParty(newUserObject)
      .subscribe(data => {   
        if (data['status'].toString() === '1') {
          issuccess = false;
        } else {
          issuccess = true;
        }
      },
        (er) => {

          issuccess = false;
        },
     () => {return issuccess });
    }

订阅3个参数

subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void):