My main job is not in IT, but I do have a CS degree and have been programming for over 20 years (only born in 1984). I hate dealing with Servers, and want Graph Databases to be the new norm.
That will not always work. ngOnInit is NOT an async function even with async, which is why I wrote this article. It will NOT wait for the function to complete. It just allows you to use the await keyword, but it is not an async function, despite having the async keyword. The function may or may not complete in time.
true, is not async in fact, but this works because await is queuing the call, is like:
ngOnInit()
... some time later...
myFunction resolves and data is populated, so the template is unblocked.
This is like a "hack" and confusing but works because the template has to wait the data.
This scenario is not a default one, blocking the template is not a good practice but in some scenario like the init of an app maybe fits and is not necessary to use Factories.
My main job is not in IT, but I do have a CS degree and have been programming for over 20 years (only born in 1984). I hate dealing with Servers, and want Graph Databases to be the new norm.
The problem is that it is not guaranteed to be resolved before the component is initialized because it is not a REAL async function. Using zone.js will guarantee it gets resolved before the component is loaded. Any other function with async in front of it, IS a real async function. Yes, it is a hack, but sometimes there is no other way.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another example, in this case to block the template render is:
//myclass.component.ts
//myclass.component.html
or use resolvers, but I think this is less code and less hard.
That will not always work.
ngOnInit
is NOT an async function even withasync
, which is why I wrote this article. It will NOT wait for the function to complete. It just allows you to use theawait
keyword, but it is not anasync
function, despite having theasync
keyword. The function may or may not complete in time.true, is not async in fact, but this works because await is queuing the call, is like:
ngOnInit()
... some time later...
myFunction resolves and data is populated, so the template is unblocked.
This is like a "hack" and confusing but works because the template has to wait the data.
This scenario is not a default one, blocking the template is not a good practice but in some scenario like the init of an app maybe fits and is not necessary to use Factories.
The problem is that it is not guaranteed to be resolved before the component is initialized because it is not a REAL async function. Using zone.js will guarantee it gets resolved before the component is loaded. Any other function with
async
in front of it, IS a real async function. Yes, it is a hack, but sometimes there is no other way.