Skip to content

Asynchronous injection of a shared module requires signature changes #475

@Pyo25

Description

@Pyo25

I read carefully the documentation about provider and some other Github issues (typically #418). It seems the only way to inject an asynchronous module is to inject a provider and then call this provider to get the actual module. (For instance: http://stackoverflow.com/questions/40880447/how-to-inject-an-asynchronous-dependency-in-inversify)

However it requires the functions to be async and thus return a promise.

In my use-case we're defining a Config class that needs to be initialized asynchronously (typically, the config will fetch values from an external service). Almost all the other modules require this config. Therefore it is not a viable solution to change all the public methods into async methods just to be able to use the await this.init(); trick.

I took the Inversify basic example and I extended it to show this use-case:
https://github.com/Pyo25/inversify-basic-example/tree/asynchronous_config_dependency

Particularly, the EpicBattle class has to return a Promise<string> but that something we should avoid. (cfr: https://github.com/Pyo25/inversify-basic-example/blob/asynchronous_config_dependency/src/entities/battle/epic_battle.ts)

So, my questions are:

  • is it the only way to inject an asynchronous dependency?
  • do you see any workaround we could use?

Thanks for your help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions