From 41d607a92fcd61dec940ed334b973f861c07f31d Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 15 Feb 2025 22:01:20 +1300 Subject: [PATCH] listen for sent and err response when sending text message --- src/connection/connection.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/connection/connection.js b/src/connection/connection.js index 3fef462..00db325 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -521,10 +521,23 @@ class Connection extends EventEmitter { return new Promise(async (resolve, reject) => { try { - // resolve with the first sent response - this.once(Constants.ResponseCodes.Sent, (response) => { + // resolve promise when we receive sent response + const onSent = (response) => { + this.off(Constants.ResponseCodes.Sent, onSent); + this.off(Constants.ResponseCodes.Err, onErr); resolve(response); - }); + } + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.Sent, onSent); + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // listen for events + this.once(Constants.ResponseCodes.Sent, onSent); + this.once(Constants.ResponseCodes.Err, onErr); // compose message const txtType = Constants.TxtTypes.Plain;