简体   繁体   中英

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.

Error: 503 RNFR required first

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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