From f977c808ecc94595cd1715666daa3379f3b39cbc Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 1 Dec 2021 17:52:09 +0100 Subject: [PATCH] Even better solution for concurrent promise handling --- app/static/js/nopaque/App.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/app/static/js/nopaque/App.js b/app/static/js/nopaque/App.js index d6c72255..6e5477a8 100644 --- a/app/static/js/nopaque/App.js +++ b/app/static/js/nopaque/App.js @@ -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) {