From 7d68b356087c259df0b879127a1dbe730a6fea9d Mon Sep 17 00:00:00 2001 From: LobeliaComputing <31212444+ibuninngu@users.noreply.github.com> Date: Sat, 17 Feb 2024 21:18:17 +0900 Subject: [PATCH] Update websocket.awaitable.js --- src/js/websocket.awaitable.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/js/websocket.awaitable.js b/src/js/websocket.awaitable.js index 10b2fc4..9c4db23 100644 --- a/src/js/websocket.awaitable.js +++ b/src/js/websocket.awaitable.js @@ -7,9 +7,11 @@ export class AwaitbleWebSocket { this.socket = new WebSocket(url); this.messagePool = {}; this.isOpen = false; + let _resolve_ = () => { }; this.socket.addEventListener("open", (event) => { this.isOpen = true; + _resolve_(this); }); this.socket.addEventListener("message", (event) => { let data = JSON.parse(event.data); @@ -26,12 +28,7 @@ export class AwaitbleWebSocket { }); return new Promise((resolve, reject) => { - let interval_id = setInterval(() => { - if (this.isOpen) { - resolve(this); - clearInterval(interval_id); - } - }, 10); + this.isOpen ? resolve(this) : _resolve_ = resolve; }); }