How to wait for a observable method to finish before proceeding in Angular 2

New Developer

I am new to Angular and I am working on fixing something that is written in Angular 2. In the method we have a call to to observable method after that we are assigning a value to a variable. I need the obersavable method to finish execution before assigning the value. Given below is the code for your reference.

Method1(): void {
     this.Service.getData()
        .subscribe(
        res => {
          
        },
        error =>  this.errorMessage = <any>error);
        this.gotData = true;
}

So, As shown above, I need to assign true to gotData variable after the getData Observable method is finished execution. But gotData is getting assigned true even before we actually get the data.

Please help me who to wait until the observable method is finished execution. Thank you !

cvb

Sounds like you need to read up on Observables and Subscriptions. Angular Docs

Alternatively, the subscribe() method can accept callback function definitions in line, for next, error, and complete handlers. For example, the following subscribe() call is the same as the one that specifies the predefined observer:

In your case the callback you have named res will be called after your observable method is finished.

So your code could be:

Method1(): void {
     this.Service.getData()
        .subscribe(
        res => {
          this.gotData = true;
        },
        error =>  this.errorMessage = <any>error
       );

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular how to wait for method to finish before proceeding to next line

How to wait for a method to finish completely before proceeding?

How to wait for getDownloadURL to finish before proceeding?

Angular2 Observable - how to wait for all function calls in a loop to end before proceeding?

How can I wait for Firestore query to finish before proceeding? (Kotlin)

Wait for AJAX to finish before proceeding with the loop?

Wait for process to finish before proceeding in Java

Wait for prior fetch to finish aborting before proceeding

powershell wait for command to finish before proceeding

JS unit testing using enzyme: how to wait for setState within component's method to finish before proceeding with test case

Angular Wait an Observable to finish before starting other Observable

Angular2 Observable - Await multiple function calls before proceeding

How to make angular wait for a function to return a value before proceeding

How to wait for new data from service before proceeding in Angular

How to convey Jmeter to wait for all samplers in a thread group to finish before proceeding to next loop

How can I wait for asynchronous callback inside Javascript synchronous loop to finish before proceeding

How do I wait for the callback passed to a Cypress custom command to finish before proceeding the test?

How do I make the current thread wait for another thread to finish before proceeding?

Wait for observable in for loop to finish before continuing loop in Angular 5

Wait for a method with axios to complete before proceeding

Have Python wait for a function to finish before proceeding with the program

In an unit test, how to wait for an RxJava2 Observable to finish before asserting results

Angular Observable wait for method to execute before return

Angular wait for template to update before proceeding with calculations?

How to wait for the future object before proceeding in flutter?

JUnit wait before proceeding

wait for result before proceeding

Angular 2+ wait for method / observable to complete

Kotlin - How do I await for this coroutine to finish before proceeding?