Async Factory
See original GitHub issueHi 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:
- Created 3 years ago
- Reactions:1
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this repo is not maintained…
Just had the same problem. You’ll get a Promise if you use it like this. I solved it like this
Works for me because im in a async function while using await