remove and prevent asynchronous @postConstruct()
functions
#10423
Labels
quality
issues related to code and application quality
@postConstruct()
functions
#10423
For a bit of context, we need post-construct functions because of property injection. In order for Inversify to inject components into an instance's properties, the instance has to be constructed. This means that injected properties are not available until after the constructor has been called.
Inversify's instantiation logic may look something like this:
The key part is that Inversify doesn't await the post-construct function. Inversify APIs are mostly synchronous.
When we define asynchronous post-construct functions, what happens is that Inversify will just invoke it, wait for the synchronous part to run and then pass the instance around assuming it is fully initialized.
As a reminder, calling an asynchronous function from a synchronous function does execute part of its body:
This will output the following:
This means that when Inversify calls our async post-construct functions, it will start passing the reference to our instance around right after the async function reaches its first
await
statement. This makes reasoning about our object's lifecycle difficult because some fields might be defined asynchronously but one might expect that Inversify waits for the post-constructor's promise to resolve.We should remove and prevent asynchronous post-construct functions to better initialize our components.
The text was updated successfully, but these errors were encountered: