Skip to content

Commit

Permalink
Add async/await to support async builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Yip committed Nov 29, 2017
1 parent 9417b75 commit 2ac83ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Container.prototype.bind = function (abstract, builder, shared) {
* @param {String} abstract
* @returns {Object}
*/
Container.prototype.make = function (abstract) {
Container.prototype.make = async function (abstract) {
debug('Make "%s"', abstract);
const name = this.getAlias(abstract);

Expand All @@ -55,7 +55,7 @@ Container.prototype.make = function (abstract) {
const constructor = this.getConstructor(name);

debug('Run constructor of "%s"', name);
const instance = constructor.call(null, this);
const instance = await constructor.call(null, this);

if (this.isShared(name)) {
debug('Register instance "%s"');
Expand Down
28 changes: 21 additions & 7 deletions src/Container.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Container Test', () => {
expect(container.bindings.foo).toEqual({ builder, shared: true });
});

it('Test make function', () => {
it('Test make function', async () => {
const container = new Container();
container.aliases.set('foo', 'bar');
container.aliases.set('build', 'make');
Expand All @@ -118,13 +118,27 @@ describe('Container Test', () => {
},
};

expect(container.make('make')).toBe('make');
expect(container.make('build')).toBe('make');
expect(container.make('bar')).toBe('foo');
expect(container.make('foo')).toBe('foo');
expect(await container.make('make')).toBe('make');
expect(await container.make('build')).toBe('make');
expect(await container.make('bar')).toBe('foo');
expect(await container.make('foo')).toBe('foo');

const result = container.make('test');
const result = await container.make('test');
expect(result).toBeInstanceOf(Date);
expect(container.make('test')).toEqual(result);
expect(await container.make('test')).toEqual(result);
});

it('Test async creater', async () => {
const container = new Container();
async function makeFoo() {
return Promise.resolve('foo');
}
container.bind('foo', async () => {
const foo = await makeFoo();
return foo;
});

const foo = await container.make('foo');
expect(foo).toBe('foo');
});
});
2 changes: 0 additions & 2 deletions src/booter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


const debug = require('debug')('node-di:boot');

function resolveConstructor(object) {
Expand Down

0 comments on commit 2ac83ad

Please sign in to comment.