I'm new to Node.js, so apologies if my question sounds somehow stupid.
Here's what I'm heading for:
I'm designing an object of which one property has to be set by database query. For this task I'm requiring promise-mysql. In order to provide the property to other objects, I would like to have a getter that checks weather the property is set or not and in the latter loads the property from the database before returning its value.
My approach so far:
MyObject = function ()
{
this._property = null;
};
MyObject.prototype._loadProperty = function()
{
var self = this;
return pool.query('SELECT * FROM table WHERE condition = ?', [condition]).then(function(rows)
{
return self._property = rows;
}).catch(function(err)
{
console.log(err);
});
};
MyObject.prototype._getProperty = function()
{
return this._property || this._loadProperty();
}
So far the getter is returning a promise object if the property has not been set/loaded yet. I can handle this promise object with ...then() in the calling function, but I would like the getter to hold back the return value until the promise has settled. Is this even possible?
Regards Greg