How can I achieve success return from the Save()
method?
public SaveItem() {
if(save()){ // The intention is to use the save method like this
// Close pop up;
}
public SaveAndNew() {
if(save()){ // The intention is to use the 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;
});
}
- If I define
save(): boolean
, it will throw an error stating thatMust return a value
. If I returnissuccess
outside the subscribe block, it will always return a false value.
How can I await the save function and return a specific value based on the response?
I have researched callbacks and they do not seem elegant. Is there a more elegant way to accomplish this?
callbacks-vs-promises-vs-rxjs-vs-async-awaits
If this were C#, I would do the following:
var isSuccess = await SaveAsync(party);