From ea7bbaa9059eda72421052fb50aae55be595f5b8 Mon Sep 17 00:00:00 2001 From: parag-pv Date: Thu, 14 Nov 2024 16:04:19 -0500 Subject: [PATCH] Change promise all to reduce for sequential resolving --- .../javascript/src/shared/Events/index.mjs | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/languages/javascript/src/shared/Events/index.mjs b/languages/javascript/src/shared/Events/index.mjs index bc458934..44e88bb0 100644 --- a/languages/javascript/src/shared/Events/index.mjs +++ b/languages/javascript/src/shared/Events/index.mjs @@ -167,22 +167,26 @@ const doListen = function(module, event, callback, context, once, internal=false reject = rej }) - if (promises.length) { - Promise.all(promises).then(responses => { - resolve(listenerId) - }).catch(error => { - // Promise.all rejects if at least one promise rejects... we don't want that behavior here - // TODO: Do something better than fail silently - if (event === '*') { - resolve(listenerId) - } - else { - // Remove the listener from external list on failure to subscribe - // TODO: Instead of removing, the failed subscription shouldn't be put into the external list - listeners.remove(listenerId) - reject(error) - } - }) + // Iterate and resolve/reject through the list of promises sequentially + const templistenerId = listenerId; + if(promises.length) { + promises.reduce((prevPromise, currentPromise) => { + return prevPromise + .then(() => currentPromise) + .then(responses => { + resolve(templistenerId) + }) + .catch(error => { + if (event === '*') { + resolve(templistenerId) + } + else { + // Remove the failed listener + doClear(templistenerId, event, context) + reject(error) + } + }); + }, Promise.resolve()); } else { resolve(listenerId)