mardi 18 juillet 2017

Were Ember Computed Properties meant to be used with / contain asynchronous code?

I'm an experienced Ember.js developer. In guides, we can find an example of Computed Property with full name (synchronous, easy, relies on first name and last name). In the wild, we can find however lots of usages of Computed Properties in an asynchronous manner (for example setting itself after promises resolve - while the first run and get returns undefined).

The more I see this asynchronous Computed Properties the more I wonder - were Computed Properties meant to be used with an asynchronous code? Isn't it asking for trouble?

A common issue is that other Computed Property (CP2) relies on async CP1. CP2 gets CP1 but it gets undefined (as CP1 will set its value in later time as it is async). CP2 finishes its calculation with the wrong value of CP2 (undefined). CP1 sets itself, but CP2 does not recalculate anymore (even CP1 changed) because CP2 isn't referenced in the template (which would mean it is bound and its value is needed all the time, always recalculates when CP1 changes) - but instead was referenced by some JavaScript call.

The real world example of this could be the calculation of total order (from e-commerce shop) price based on items in the order. Computed property relies on asynchronous relationships to items, which could contain other asynchronous relationships, like tax type.




Aucun commentaire:

Enregistrer un commentaire