#How to provider dynamic asnyc service

1 messages · Page 1 of 1 (latest)

silver marsh
#

Hi, I have this module

@Module({})
export class AMQPModule {
  static registerAsync(options: AMQPModuleAsyncOptions): DynamicModule {
    return {
      module: AMQPModule,
      imports: options.imports,
      providers: [{ provide: AMQP_CONNECTION_OPTIONS, useFactory: options.useFactory, inject: options.inject }, AMQPService],
      exports: [AMQPService],
    };
  }
}

and I want AMQPService to first connect with server and then hold this connection but with the code above it doesn't happen even that AMQPService constructor envokes async method with server connection. Is it possible to export AMQPService as prepared value like

exports: [
  {
    value: async () => {
      const service = new AMQPServce();

      await service.connect();

      return service;
    }
  }
]

?

marble swan
#

You can't use async methods in the constructor. But you can use the onModuleInit or onApplicationBootstrap methods to perform async cpnfiguration