How to wait for function with subscribe to finish in angular 8?
Asked Answered
M

3

9

I want to wait for one function to finish before executing the function next to it. I have one function called getData() in which http call occurs which returns an observable. The second function checkDuplicate() we have subscribed to that function getData() . and we have third function called proceed() in which we call the checkDuplicate() function and once the checkDuplicate() function is completed, we are calling alert("finished"). But the issue here is even before checkDuplicate() function is completed, alert has been triggered.

find the code for better clarification:

getData(): Observable<any>{
  return this.http.get<any>(URL...);
}

checkDuplicate(){

return this.getData().subscribe(response => {
   if(response){
      console.log("Inside");
  }
});
}

proceed(){
 this.checkDuplicate();
 console.log("finished");
}

Actual Result

finished

Inside

Expected result

Inside

finished

I have tried asyn/await to wait for checkDuplicate() function to finish. But still no use. It would be grateful if u share the solution. Thanks in Advance !!!

Monika answered 28/7, 2020 at 14:52 Comment(0)
V
8

The gotcha of async programming. Here's a solution with promises (you can also use observables):

getData(): Observable<any>{
  return this.http.get<any>(URL...);
}

async checkDuplicate(){
  var response = await this.getData().toPromise();
  if (response) {
      console.log("Inside");
  }
}

async proceed(){
 await this.checkDuplicate();
 console.log("finished");
}
Vellavelleity answered 29/7, 2020 at 7:46 Comment(6)
Thanks for ur answer @Andrei Tătar !! But still am getting the same issueMonika
@sherinshaf you're probably using different code. Post your complete code.Kasper
Same code. As soon as i call await this.getData().toPromise(); control goes to console.log("finished") in proceed() function.. its not waiting for responseMonika
have you copied all the code from my response? proceed also has async/awaitKasper
What about in we are in an interceptor that it is not async method?Neckcloth
@GabrielGarcíaGarrido http interceptors can return an Observable or a Promise so they are asynchronous. You can use Promises (async/await) together with Observables...Kasper
G
1

Lost a lot of time on this one. You have to use lastValueFrom as specified here: Conversion to Promises. ToPromise() has been deprecated.

import { lastValueFrom, tap } from 'rxjs';

getData(): Observable<any>{
  return this.http.get<any>(URL...);
}

async checkDuplicate(){
  return await lastValueFrom(this.getData()
    .pipe(tap(response => {
      if(response){
        console.log("Inside");
      }
    })
  ));
}

async proceed(){
  await this.checkDuplicate();
  console.log("finished");
}
Greerson answered 27/4, 2023 at 10:29 Comment(0)
U
0

You can update something like this.

getData(): Observable<any>{
return this.http.get<any>(URL...);
}


checkDuplicate() {
return Promise(resolve=>{
this.getData().subscribe(response => {
if(response){
console.log("Inside");
 }
resolve(true);
});
});
}


proceed(){
this.checkDuplicate().then(value=>{
console.log("Return from promise => "+ value);
console.log("finished");
});
}

You can return promise from checkDuplicate and call another function after it is resolved by using "then".

Undersecretary answered 13/5, 2022 at 3:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.