如何使此代码 运行 在 Node.Js 中同步?

How can make this code run synchronous in Node.Js?

我想使用 EasyFTP 移动文件,但如果我关闭连接,它会在移动任何文件之前关闭,如果我不关闭连接,我会收到错误消息.

错误:首先需要 503 RNFR

这是我的代码

var EasyFtp = require('easy-ftp');
var ftp = new EasyFTP();
var config = {
    host: '',
    port: 21,
    username: '',
    password: ''
};

ftp.connect(config);

 var filesFrom=['/file1.txt','/anotherFile.txt','/moreFiles.txt','/a.txt','/x.txt']
 var filesTo=['/archived/file1.txt','/archived/anotherFile.txt','/archived/moreFiles.txt','/archived/a.txt','/archived/x.txt']

 for (var i = 0; i < filesFrom.length; i++) {
    ftp.mv(filesFrom[i], filesTo[i], function(err, newPath){
        if (err) { console.log(err) }
    });
 };

ftp.close();

您无法在 Javascript 中同步制作异步内容 运行。而且,由于 for 循环是同步的,您不能让 for 循环等待异步操作完成后再进行下一次迭代。因此,相反,您必须为迭代使用不同的技术。有许多不同的选择。这是一个手动迭代的选项,当上一个迭代完成时触发下一个迭代:

function mvFiles(ftpObj, fromArray, toArray, callback) {
    let index = 0;
    let results = [];

    function next() {
        if (index < fromArray.length) {
            let i = index++;
            ftpObj.mv(fromArray[i], toArray[i], function(err, newPath) {
                if (err) {
                    callback(err);
                } else {
                    results[i] = newPath;
                    // run next iteration now
                    next();
                }
            });
        } else {
            // all done
            callback(null, results);
        }
    }

    // start first iteration
    next();
}

用法:

ftp.connect(config);

var filesFrom =['/file1.txt','/anotherFile.txt','/moreFiles.txt','/a.txt','/x.txt'];
var filesTo =['/archived/file1.txt','/archived/anotherFile.txt','/archived/moreFiles.txt','/archived/a.txt','/archived/x.txt'];

mvFiles(ftp, filesFrom, filesTo, function(err, newPaths) {
    ftp.close();
    if (err) {
        // process error here
    } else {
        // all done here
    }
});

使用 Bluebird 很容易

var Bluebird = require('Bluebird');
var EasyFtp = require('easy-ftp');
var ftp = new EasyFTP();
var config = {
    host: '',
    port: 21,
    username: '',
    password: ''
};

// Promisifying adds Async after every method which represents the promise version of that method... you don't have to follow the callback method.
Bluebird.promisifyAll(ftp);

ftp.connect(config);

// push your promises into an array and then Promise.all() it... It will either complete fully or throw an error even if one fails.... All or nothing.

var promises = [];

 var filesFrom=['/file1.txt','/anotherFile.txt','/moreFiles.txt','/a.txt','/x.txt']
 var filesTo=['/archived/file1.txt','/archived/anotherFile.txt','/archived/moreFiles.txt','/archived/a.txt','/archived/x.txt']

for (var i = 0; i < filesFrom.length; i++) {
    promises.push(ftp.mvAsync(filesFrom[i], filesTo[i]))
};

// Now promises array contains all the promises and they have started executing.

Bluebird.all(promises).then(function(results) {

     // Results is an array of results from all the promises in order.

     console.log(results);

     // Close connection.
     ftp.close();
}).catch(function(err) {
     console.log(err);
});