diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index 1e8f8ce9c..2478da4b2 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -765,11 +765,23 @@ module.exports = function(RED) { } callback && typeof callback == "function" && callback(); }; - if(node.closing) { - return _callback(false, force); - } - var endCallBack = function endCallBack() { - } + if(!node.client) { return _callback(); } + if(node.closing) { return _callback(); } + + let waitEnd = (client, ms) => { + return new Promise( (resolve, reject) => { + node.closing = true; + if(!client) { + resolve(); + } else { + const t = setTimeout(reject, ms); + client.end(true, () => { + clearTimeout(t); + resolve() + }); + } + }); + }; if(node.connected && node.closeMessage) { node.publish(node.closeMessage, function (err) { node.client.end(endCallBack);