Service async Initializer called multiple times (v3)
See original GitHub issueWhile upgrading packages in one of my projects today, I noticed that starting from version 3.13 async initializer gets called multiple times (looks like once per injection?).
I’m wrapping knex as follows:
import { FastifyInstance } from 'fastify';
import { FastifyInstanceToken, Initializer, Inject, Service } from 'fastify-decorators';
import { Knex } from 'knex';
import { Database } from '@/database/Database';
@Service()
export class DatabaseService {
readonly connection!: Knex;
@Inject(FastifyInstanceToken)
readonly instance!: FastifyInstance;
public constructor() {
this.connection = Database.getInstance().connection;
this.instance.addHook('onClose', this.onClose.bind(this));
}
/**
* This gets called on each @Inject call?
* x1: Service A -> DatabaseService
* x1: Service B -> Service A -> DatabaseService
* etc
*/
@Initializer()
public async init() {
await this.connection.raw(`select current_database()`);
}
private async onClose() {
await this.connection.destroy();
}
}
Nothing in documentation indicates that I should keep track on initialization myself (idempotent initializer), so I’m assuming its a bug?
Issue Analytics
- State:
- Created a year ago
- Comments:19 (16 by maintainers)
Top Results From Across the Web
c# - Calling an async method multiple times - Stack Overflow
The problem is that the application can call the Initialise method multiple times (user moving around randomly) passing in new set of ...
Read more >How to do asynchronous operation in object constructor
... returning Promise Approach 1 - Start call in constructor, await completion in method Approach 2 - Use async initializer() method Approach 3...
Read more >The Proper Way to Write Async Constructors in JavaScript
Produces verbose initialization at the call site. Requires the caller to be familiar with the lifecycle semantics and internals of the class.
Read more >Why Is My Future/Async Called Multiple Times? - Flutter Igniter
Here we have a sample parent widget that rebuilds every 3 seconds. It's meant to represent any widget that triggers rebuilds like, for...
Read more >Async/Await - Best Practices in Asynchronous Programming
Async methods returning void don't provide an easy way to notify the calling code that they've completed. It's easy to start several async...
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

@L2jLiga Last version seems to fix all issues, I didn’t notice anything new. Thanks!
So created a repro here.
and
gives me
While console: