DEV Community

Discussion on: please help me with this problem

Collapse
 
bradtaniguchi profile image
Brad • Edited

I added an thorough answer on SO, I'll add the TLDR version here:

This code wont work as expected

this.restApiOwner.getOwners().subscribe(Owners=> this.owners=Owners);
console.log(this.owners);

it should be this:

this.restApiOwner.getOwners().subscribe(Owners=> {
  this.owners=Owners;
  console.log(this.owners);
});

If the getOwners() call takes say 5 seconds to return, JS wont wait, and will keep executing code after it, only calling the subscribe callback once the data comes back. As I mentioned in the reply I recommend reading about how JS handles async stuff, as its key to the language.