Javascript APIs with async properties

omrilotan
omrilotan
Feb 11, 2018 · 2 min read

Getter properties are a great way to mask logic and expose what appears as static values. I find it extremely elegant to differentiate functionality from attributes. This logic may hide lazy calculations to be performed only on-demand, or it may hide logic that’s based on object’s, or even application state.

For example, a User object may have functionality like user.goOnline(), it should have attributes like user.isOnline. The check if a user is online should be performed on demand because this status may change from moment of instantiation to the moment of querying.

With the emerging of async/await in Javascript, it is now possible to create such lazy getters by pointing them to promises, creating more semantic APIs.


Static object can also leverage this way of typing. An API object pointing to async properties may also appear more readable.

When importing between modules — I feel the appeal is even greater.

And this just opens up endless sugary syntactic possibilities.


The following is a (simplified) real life example where I found using async getters made the final logic cleaner.
`git tag -a ${tag} -m "${await message}"`

 by the author.

omrilotan

Written by

omrilotan

Moved to https://dev.to/omrilotan

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade