Попробуйте следующий код ...
var Client = require("ftp");
numAddresses = Object.keys(config.addresses).length;
clients = [];
for (address in config.addresses) {
// current address being processed
console.log(config.addresses[address]);
// create a new client for this address
clients[address] = createClient(path);
// update the settings for this new client based on address
// note we keep the default anonymous login credential
// you can change it here though
connectionSettings.host = config.addresses[address].ip;
connectionSettings.port = config.addresses[address].port;
console.log(connectionSettings);
// connect the current client with the associated settings
clients[address].connect(connectionSettings);
}
function createClient(path) {
var c = new Client();
c.on('greeting', (msg) => {
afterGreeting(msg, c);
});
return c;
}
function afterGreeting(msg, client) {
console.log(msg);
client.on('ready', () => {
afterReady(client);
});
}
function afterReady(client) {
console.log('ready');
performListing(path, client);
}
function performListing(path, client) {
console.log('fetching from:' + path);
client.list(path, (err, list) => {
performFetch(err, list, client);
});
}
function performFetch(err, list, client) {
if (err) throw err;
list.forEach(function (element, index, array) {
console.log('now copying: ' + element.name);
});
client.end();
}
Обновлено
var Client = require("ftp");
numAddresses = Object.keys(config.addresses).length;
clients = [];
for (address in config.addresses) {
// current address being processed
console.log(config.addresses[address]);
// create a new client for this address
clients[address] = createClient(path);
// update the settings for this new client based on address
// note we keep the default anonymous login credential
// you can change it here though
connectionSettings.host = config.addresses[address].ip;
connectionSettings.port = config.addresses[address].port;
console.log(connectionSettings);
// connect the current client with the associated settings
clients[address].connect(connectionSettings);
}
function createClient(path) {
// initialize new client
var c = new Client();
// official npm documentation https://www.npmjs.com/package/ftp#events
// tells us, that greeting returns a string so we need to pass a callback
// to c.on('greeting', callback)
// we could either write function(msg) { } or short form (msg) => { }
c.on('greeting', (msg) => {
afterGreeting(msg, c);
});
return c;
}
function afterGreeting(msg, client) {
console.log(msg);
// ready needs a callback too but returns nothing, only calls this callback
// if ready is ready. Kappa.
client.on('ready', () => {
afterReady(client);
});
}
function afterReady(client) {
console.log('ready');
performListing(path, client);
}
function performListing(path, client) {
console.log('fetching from:' + path);
// client.list returns an err if we encounter an error and a list
// our callback is written in shortform (err, list) => { } so we're
// passing those callback data to this anonymous function
client.list(path, (err, list) => {
performFetch(err, list, client);
});
}
function performFetch(err, list, client) {
if (err) throw err;
list.forEach(function (element, index, array) {
console.log('now copying: ' + element.name);
});
client.end();
}
Ответ на вопрос : Зависит от вашей программы.Вы всегда можете переписать и оптимизировать программу.