简体   繁体   English

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

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

I Want to Move files using EasyFTP , but if i close the connection, it closes before moving any file, and if i don't close the connection i get an error. 我想使用EasyFTP移动文件,但是如果我关闭连接,则它在移动任何文件之前都会关闭,如果我不关闭连接,则会出现错误。

Error: 503 RNFR required first 错误:首先需要503 RNFR

So here's my code 所以这是我的代码

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

You can't make asynchronous things run synchronously in Javascript. 您无法使异步事物在Javascript中同步运行。 And, since a for loop is synchronous, you can't make a for loop wait for an asynchronous operation to complete before doing the next iteration. 而且,由于for循环是同步的,因此在进行下一次迭代之前,您不能让for循环等待异步操作完成。 So, instead, you have to use a different technique for your iteration. 因此,您必须为迭代使用其他技术。 There are many different options. 有很多不同的选择。 Here's one option that iterates manually, triggering the next iteration when the previous one is done: 这是一个手动迭代的选项,在上一个完成后触发下一个迭代:

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

Usage: 用法:

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

It's easy with 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM