Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Factory #64

Open
davidbayo10 opened this issue Apr 21, 2020 · 5 comments
Open

Async Factory #64

davidbayo10 opened this issue Apr 21, 2020 · 5 comments

Comments

@davidbayo10
Copy link

Hi all! I want to perform this factory class behaviour, but when I call new S(), a promise is returned. Do I do something wrong? May this feature is not present for being used as factory?

const factory: ObjectFactory = async () => S.getInstance();

@Factory(factory)
export class S {

  private static _instance: S;

  static async getInstance(): Promise<S> {
    if (!this._instance) {
      this._instance = new S();

      await this._instance.createClient();
    }

    return this._instance;
  }

  async createClient(): Promise<void> {
    this._client = await createClientAsync('url');
  }

}

Thank you all in advance!

@InsOpDe
Copy link

InsOpDe commented Aug 3, 2020

Just had the same problem. You'll get a Promise if you use it like this. I solved it like this

const messenger = await Messenger.create(uri, serviceName, ttl)
Container
		.bind(Messenger)
		.factory(() => messenger)
		.scope(Scope.Singleton)

Works for me because im in a async function while using await

@TheOnlyMrFlow
Copy link

Any update on this ?

@davidbayo10
Copy link
Author

Just had the same problem. You'll get a Promise if you use it like this. I solved it like this

const messenger = await Messenger.create(uri, serviceName, ttl)
Container
		.bind(Messenger)
		.factory(() => messenger)
		.scope(Scope.Singleton)

Works for me because im in a async function while using await

I prefer to use @Inject into property classes

@davidbayo10
Copy link
Author

Any update on this ?

I think this repo is not maintained...

@tripodsgames
Copy link

I'm using async-injection instead of this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants