Your subscribe function is designed to be asynchronous, meaning that when the
this.featureService.upVoteAvailable(i)
method is invoked, the code will continue executing without waiting for the Observable result.
As a result, you must wait for the Observable to provide its response before proceeding.
There are multiple methods available to handle this situation.
One approach is to utilize async/await functionality.
async checkUpVoteAvailability(i: number) {
let isAvailable!:boolean;
await this.featureService.upVoteAvailable(i).toPromise.then(response => {
isAvailable = response as boolean;
console.log(isAvailable); //true
})
return isAvailable; //true
}
By using async/await, the execution is paused until the desired value is received. This method relies on Promises for handling asynchronous operations.