I have implemented this function in one of my servlets:
private setValues() {
this.config.socket.on('config.weather', (values:any) => {
console.log(values);
}
However, I would like to refactor it to something like this:
private setValues() {
this.config.load('weather').then((values:any) => {
console.log(values);
}
This is the new method implementation in the socket Service:
public load(key: string) {
return new Promise(resolve => {
this.socket.on('config.' + key, values => resolve(values));
});
Although this solution works for the initial call, subsequent socket triggers do not send data to the setValues()
function as Promises only work once.
I believe I need to use an Observable instead, but I am unsure where to integrate it within the code.