Skip to content
Snippets Groups Projects
Commit f977c808 authored by Patrick Jentsch's avatar Patrick Jentsch
Browse files

Even better solution for concurrent promise handling

parent ff514368
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ class App {
constructor() {
this.data = {users: {}};
this.eventListeners = {'users.patch': []};
this.promises = {users: {}};
this.socket = io({transports: ['websocket'], upgrade: false});
this.socket.on('users.patch', patch => this.usersPatchHandler(patch));
}
......@@ -51,16 +52,10 @@ class App {
}
getUserById(userId) {
let promise;
if (userId in this.data.users) {
if (this.data.users[userId] instanceof Promise) {
return this.data.users[userId];
} else {
return new Promise((resolve, reject) => {resolve(userValue);});
}
if (userId in this.promises.users) {
return this.promises.users[userId];
}
promise = new Promise((resolve, reject) => {
this.promises.users[userId] = new Promise((resolve, reject) => {
this.socket.emit('users.user.get', userId, response => {
if (response.code === 200) {
this.data.users[userId] = response.payload;
......@@ -70,8 +65,7 @@ class App {
}
});
});
this.data.users[userId] = promise;
return promise;
return this.promises.users[userId];
}
usersPatchHandler(patch) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment