DEV Community

Acewings
Acewings

Posted on

please help me with this problem

please help me with this problem
https://stackoverflow.com/questions/58768962/how-to-fix-the-problem-of-type-compatibility-in-typescript-when-trying-to-consum

Top comments (3)

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.

Collapse
 
devdrake0 profile image
Si

In future, please don't just paste a link to Stack Overflow or the #help tag will be removed.

At least provide a short summary of what your issue is.

Thanks.

Collapse
 
khaireddines profile image
Acewings

ah ok am sorry new here i will make sure to do that in the future ^