Class decorators and async process

Gray Zhang otakustay at
Sat May 23 16:47:09 UTC 2015

Hi all:

I’m wondering if there is any way to combine a class decorator and an async process together? A common case would be a generic IoC implementation:

Since in JavaScript IoC we load runtime modules async by a config file, the interface may be:

{Promise} ioc.getComponent({string} componentName)

which resolves the returned Promise giving required instance of componentName, and this is a method that cannot be sync since we need a config file to map componentName to its implementing module and load the module lazily (for performance reason)

This is an async process so if we add a decorator to a class property:

function inject(name) {
    return (target, key, descriptor) {
        // Note this returns a promise, not the actual property value
        descriptor.initializer = () => ioc.getComponent(name);

class Hero {
    weapon = null
    hit(enemy) {
        enemy.heath -= (this.weapon.power - enemy.defense);

This code may not work, but nobody likes there injected properties to be all async getters and all code logics become unnecessarily complex by introducing so many async processes

How so we think of such common case, should I just make the ioc.getComponent sync without considerations to performance, or should I give up the decorator solution?


Best regards

Gray Zhang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list