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

时间:2016-12-09 23:40:31

标签: node.js ftp-client

我想使用 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();

2 个答案:

答案 0 :(得分:1)

您不能在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
    }
});

答案 1 :(得分:0)

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);
});