-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding 'has' method #45
Adding 'has' method #45
Conversation
Since version With your solution ( Example: function loadPreferencesForModule( moduleName ) {
var key = "arbitraryPrefixString:" + moduleName;
// get key and return it as Promise if existend
var _val = myCache.get(key);
// check for an existing value
if ( typeof _val === "undefined" || _val === null ) {
var promise =
fetchDataFromSomewhereQuiteSlow()
.then( extractAKeyFromTheDataThatMightBeMissing );
.then( function ( value ) {
return myCache.set(key, value);
});
} else {
return new Promise(function(resolve, reject) {
// return the already received value
resolve( _val );
});
}
} |
Hi! Thanks for the example, but the problem is, what if I see your point about the race condition though; maybe an option to |
Now i understand your problem. What if you prevent to store function loadPreferencesForModule( moduleName ) {
var key = "arbitraryPrefixString:" + moduleName;
// get key and return it as Promise if existend
var _val = myCache.get(key);
// check if the value is undefined
if ( _val === undefined ) {
var promise =
fetchDataFromSomewhereQuiteSlow()
.then( extractAKeyFromTheDataThatMightBeMissing );
.then( function ( value ) {
// prevent storing undefined by converting it to null
return myCache.set(key, ( value === undefined ? null : value ) );
});
} else {
return new Promise(function(resolve, reject) {
// return the already received value
resolve( _val );
});
}
}
I will think about and discus adding some code like What do you think? |
That would work for a one-off, but it'd be better if the logic for that could be kept inside the cache; you can imagine someone might simply forget to put in the 'swap undefined to null' check. I've opened a PR for a throwOnMissing option at #46 , which I think makes more sense with reference to the race condition you mentioned. |
Hi, An i refactored the error handling and used the existing |
Hope that's OK. |
Adding the
.has
method, used to ensure a key is available, to handle cases where.set("key", foo.bar)
might result in a falsy value. EG:fetchDataFromSomewhereQuiteSlow
, then extracted the preferences for a module that hasn't had any preferences set yet, the correct value would beundefined
. Rather than wait for the preferences to load every time we want to know about this module's preferences, we would want to avoid the call over the network and just returnundefined
until the cached value expires.( If this is merged, please be aware I have assumed the next version is 3.0.2 in the
README.md
file. )