0

This is My Code: getTransaction(transactionRef: string){

    this.partnerportalService
        .getTransaction(transactionRef)
        .subscribe(data=>{
            this.transactionData = data;
            console.log("Return the Data ", this.transactionData);
        });
    console.log("Return Undefined", this.transactionData);

}
  • yes because http observables are asynchronous. – Aakash Garg May 06 '22 at 12:23
  • It is an async function. You need to place the content inside the subscription. – Swapnil Sourabh May 06 '22 at 12:25
  • Does this answer your question? [How do I return the response from an Observable/http/async call in angular?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular) – R. Richards May 06 '22 at 12:34

1 Answers1

1

.subscribe is asynchronous, meaning that it is going to be executed sometime later, however the line that contains console.log("Return Undefined", this.transactionData); is outside of the subscribe function scope and is instead executed synchronously. This means that console.log("Return Undefined", this.transactionData); will execute before this.transactionData = data;, hence it prints undefined.

Ovidijus Parsiunas
  • 2,512
  • 2
  • 8
  • 18