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;