From bad7d52e49e45c76f06b30c347674883c3f05c54 Mon Sep 17 00:00:00 2001 From: "chenyong.heyo" Date: Mon, 15 Apr 2019 11:44:36 +0800 Subject: [PATCH] fix(connect): remove listener after connect successfull --- src/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 3184711..80f6484 100644 --- a/src/index.js +++ b/src/index.js @@ -62,7 +62,7 @@ SftpClient.prototype.list = function(path) { /** * @async - + * Tests to see if an object exists. If it does, return the type of that object * (in the format returned by list). If it does not exist, return false. * @@ -547,7 +547,7 @@ SftpClient.prototype.connect = function(config, connectMethod) { connectMethod = connectMethod || 'on'; return new Promise((resolve, reject) => { - this.client[connectMethod]('ready', () => { + const readyListener = () => { this.client.sftp((err, sftp) => { this.client.removeListener('error', reject); this.client.removeListener('end', reject); @@ -555,9 +555,11 @@ SftpClient.prototype.connect = function(config, connectMethod) { reject(new Error(`Failed to connect to server: ${err.message}`)); } this.sftp = sftp; + this.client.removeListener('ready', readyListener); resolve(sftp); }); - }) + }; + this.client[connectMethod]('ready', readyListener) .on('end', reject) .on('error', reject) .connect(config);