rawmodel / framework

Strongly-typed JavaScript object with support for validation and error handling.
https://rawmodel.github.io/framework/
MIT License
142 stars 16 forks source link

Async getter support #81

Closed BerciTheBeast closed 4 years ago

BerciTheBeast commented 4 years ago

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.

xpepermint commented 4 years ago

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.