Your question basically translate to:
What is the best way to jump off a sky scraper into a tank full of
sharks with lasers on their heads.
The answer would basically be: "You don't"
Why ECMAScript uses callbacks and promises and async await syntax (is basically promises with a different syntax) is explained here with links to documents and a very good video.
Assuming you are using this memecached client you can set the value member to a promise, the code is kind of confusing since you don't set value when you call setItem. GetItem sets value but I think it's better if you name it lastRetreivedValue (and maybe add lastSetItem depending on your needs):
class TPCacheManager{
constructor(){
this.lastRetrievedValue = Promise.reject("No item has been set");
}
setItem(type,key,item){
var mcacheClient = new MemcachePlus();
//the caller may want to know when it's finished and if it failed
return mcacheClient.set("TP:IDX","3");
}
getItem(key){
var mcacheClient = new MemcachePlus();
const me = this;
//caller may want to know when it's finished and if it failed
return mcacheClient.get("TP:IDX").
then(value => {
console.log(value);
me.lastRetrievedValue= value;
//you may want to return value here so o.getItem().then(value
//actually resolves to something other than undefinded
});
}
theVal(){
return this.lastRetrievedValue;
}
}
//example how to use:
const cache = new TPCacheManager();
cache.lastRetrievedValue.catch(
err=>{
console.log("As expected, no value has been set",err);
}
);
cache.setItem("doesnt matter, you ignore all parameters here")
.catch(err=>console.error("Oops, something went wrong setting an item:",err));
cache.lastRetrievedValue
.catch(
err=>
console.log("Value still not set, setItem does not do this in your code",err)
);
cache.getItem("doesnt matter, you are ignoring parameters here")
.then(
value=>
console.log("Value is undefined because getItem resolves to undefined",value)
);
cache.lastRetrievedValue
.then(
value=>
console.log("Ok, I have value because getItem has set it:",value)
);
//you can repeat cache.lastRetrievedValueor cache.theVal() without connecting to
// memcached because you stored the promise