I start learning angular with typescript for a while and now I face a situation that I couldn't figure out how I should solve. I have two methods in a class method A and method B. Both of them send a request to the database to receive some information. the problem is when I call method B inside method A the value that I receive from method B is not updated, it shows me the value for the previous call, how I can overcome this situation so when I call method B in method A it shows me the fresh result.
@Injectable()
export class foo{
id: number;
async method_A(){
this.method_B();
console.log(this.id); // in here after I call method_B it should show me the correct value
// but it will show me the value for previous call to database.
await this.http.get<any>("url").subscribe(data => {})
}
method_B(){
this.http.get<any>("url").subscribe(data => {
this.id = data.id;
})
}
}
things that I tried so far:
First, I used await when I call the method_B but it doesn't work.
Second, I tried to use a BehaviorSubject and assign the id in it and return it as an asObservable and subscribe to it inside method_A, but still the result is not what I want. in this case, the method will call twice and the first time the value is the old value, and the second time it will return the updated value which is not useful for me because in another file I always receive the value from the first time call.