question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
davidbayo10commented, Feb 2, 2021

Any update on this ?

I think this repo is not maintained…

1reaction
InsOpDecommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async factory in Simple Injector - Stack Overflow
Before Object Composition means during application startup, which means one-time start-up initialization, while async operations that happen ...
Read more >
Async providers | NestJS - A progressive Node.js framework
The factory returns a Promise , and the factory function can await asynchronous tasks. Nest will await resolution of the promise before instantiating...
Read more >
The Proper Way to Write Async Constructors in JavaScript
The static async factory function pattern allows us to emulate asynchronous constructors in JavaScript. At the core of this pattern is the ...
Read more >
How to fully initialize types in their constructor with C# nullable ...
This blog gives an example of a pattern used for asynchronous initialisation. ... To do this we can use the async factory method...
Read more >
Async Programming - Patterns for Asynchronous MVVM ...
The first time the AsyncLazy<T> instance is awaited, it will start the asynchronous factory method once on a thread pool thread. Any other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found