Returning value from subscription typescript

走远了吗. 提交于 2019-12-03 21:03:34

you can make your save method return an observable of boolean value

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)); 
}

How about something like this:

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
}

Or ... TypeScript now supports async/await ... so you could consider using them here. See this for more information: https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875

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

So, your service would be like

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

and retrieve that looks like

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

                return issuccess = false;
            });
    }

try it like

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 ;
}
Parameswar Rao

We have another action parameter in subscribe() after er action that will help you to return without using observable.

  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 });
    }

Subsribe 3 paramters

subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!