Gray Zhang (2015-05-24T03:38:26.000Z)
dignifiedquire at gmail.com (2015-05-30T22:50:26.047Z)
Sorry for some confusing descriptions, IoC means Inversion of Control here, I’d like to put all code together here: ```js // A simple implement of IoC ioc.getComponent = (name) { return new Promise((resolve) => { let iocConfig = parseConfig(name); let moduleId = iocConfig.module; require([moduleId], function (ModuleClass) { let instance = new ModuleClass(); injectProperties(instance, iocConfig); resolve(instance); }); }); } let inject = (name) => { return (target, key, descriptor) => { // target = hero // key = 'weapon' // Use initializer to get the property value descriptor.initializer = () => { return ioc.getComponent(name); }; }; }; class Hero { // We need a knife from ioc as this hero's weapon @inject('knife') weapon = null; hit(enemy) { // Problem here enemy.heath -= this.weapon.power - enemy.defense; } } ``` The problem is, as I know, when a decorator implements the descriptor.initializer the property value should be the return value of this initializer, however the initializer should be sync which directly returns the value but not a promise or a async function, we can’t await for a descriptor.initializer In this case, in my hit method, the this.weapon is a Promise but not a knife instance, Promise does not have e a power property so hit method fails, I may correct the code: ```js hit(enemy) { return this.weapon.then((weapon) => { emeny.heath -= weapon.power - enemy.defense; }); } ``` It’s OK, I just wait for this.weapon to resolve, but then my hit method becomes async, and everything based on @inject property should be async, which is not actually what I want. Is the decorator idea based on extended property descriptors actually being used, e.g., with Babel? Sorry if I missed it. Decorator is now supported with Babel 5.0 and I was trying this these days, expecting it could be integrated with our IoC framework, just as how Spring works in Java. Best Gray Zhang