rawmodel/framework

Async getter support

BerciTheBeast opened this issue · 1 comments

I propose the addition of having optional async getters.

For example, the following code would be possible and would work:

@prop(...)
public image: string;

@prop({
...
    getter() {
        return getImageUrl(this.image); // call to async function that generates link on the fly
    }
})

This would likely also impact serialization.

Thanks for the proposal @BerciTheBeast. I'm afraid this won't work. Getters and setters can not be async because they are "a replacement" for variable default getter and setter.

var user;
user = 'foo'; // valid
await user = "foo"; // invalid

It's always good practice to have deterministic objects where you also explain what's happening. For example:

model.firstName // a value is present, ok
await model.getRemoveImage() // async process where value is not yet present, ok

What I usually do in your case is this:

const user = new User(); // model instance
await user.loadRemoveImage(); // get remove image and populate the user.image property
user.serialize(); // now I have all the data

I hope this helps.