diff --git a/src/container-instance.class.ts b/src/container-instance.class.ts index d5c91237..b0005f25 100644 --- a/src/container-instance.class.ts +++ b/src/container-instance.class.ts @@ -8,7 +8,6 @@ import { ServiceMetadata } from './interfaces/service-metadata.interface.'; import { AsyncInitializedService } from './types/AsyncInitializedService'; import { MissingInitializedPromiseError } from './error/MissingInitializedPromiseError'; - /** * TypeDI can have multiple containers. * One container is ContainerInstance. @@ -122,7 +121,7 @@ export class ContainerInstance { * Like get, but returns a promise of a service that recursively resolves async properties. * Used when service defined with asyncInitialization: true flag. */ - getAsync(type: ObjectType): Promise; + getAsync(type: Constructable): Promise; /** * Like get, but returns a promise of a service that recursively resolves async properties. @@ -498,9 +497,9 @@ export class ContainerInstance { if (type) this.applyPropertyHandlers(type, value); if (value instanceof AsyncInitializedService || service.asyncInitialization) { - return new Promise((resolve) => { + return new Promise(resolve => { if (!(value.initialized instanceof Promise) && service.asyncInitialization) { - throw new MissingInitializedPromiseError(service.value); + throw new MissingInitializedPromiseError(service.value); } value.initialized.then(() => resolve(value)); diff --git a/src/container.class.ts b/src/container.class.ts index 1faf2865..3bbe4ce5 100644 --- a/src/container.class.ts +++ b/src/container.class.ts @@ -5,7 +5,6 @@ import { Constructable } from './types/constructable.type'; import { ServiceIdentifier } from './types/service-identifier.type'; import { ServiceMetadata } from './interfaces/service-metadata.interface.'; - /** * Service container. */ @@ -110,7 +109,7 @@ export class Container { * Like get, but returns a promise of a service that recursively resolves async properties. * Used when service defined with asyncInitialization: true flag. */ - static getAsync(type: ObjectType): Promise; + static getAsync(type: Constructable): Promise; /** * Like get, but returns a promise of a service that recursively resolves async properties. diff --git a/test/decorators/Service.spec.ts b/test/decorators/Service.spec.ts index ba5c601a..e09a6568 100644 --- a/test/decorators/Service.spec.ts +++ b/test/decorators/Service.spec.ts @@ -172,35 +172,35 @@ describe('Service Decorator', function () { }); expect(Container.get(TestService)).toBe('TEST_STRING'); + }); + + it('should support services with asynchronous initialization', async function () { + @Service({ asyncInitialization: true }) + class Engine { + ignition: string = 'off'; + initialized: Promise; - it('should support services with asynchronous initialization', async function () { - @Service({ asyncInitialization: true }) - class Engine { - ignition: string = 'off'; - initialized: Promise; - - constructor() { - this.initialized = this.initialize(); - } - - protected initialize() { - return new Promise(resolve => { - setTimeout(() => { - this.ignition = 'running'; - resolve(); - }, 300); - }); - } + constructor() { + this.initialized = this.initialize(); } - @Service() - class Car { - constructor(public engine: Engine) {} + protected initialize() { + return new Promise(resolve => { + setTimeout(() => { + this.ignition = 'running'; + resolve(); + }, 300); + }); } + } + + @Service() + class Car { + constructor(public engine: Engine) {} + } - const car = await Container.getAsync(Car); + const car = await Container.getAsync(Car); - expect(car.engine.ignition).toEqual('running'); - }); + expect(car.engine.ignition).toEqual('running'); }); });