NodeJs中的瀑布似乎是异步的。为什么?

时间:2016-07-29 10:10:32

标签: node.js

我在循环中为每个配置文件运行瀑布。在瀑布的第一个功能中,我正在从SFTP下载文件。从我读到的有关瀑布的内容来看,它应该等到一个函数完成后再开始下一个函数。

对我来说恰恰相反。

在循环中为每个配置文件调用瀑布函数:

function waterfallMain(configFile, i) {
  async.waterfall([
    function(callback) {
      // If there is no such file error is thrown and next iteration of the loop starts
      util.log("Downloading config files."+i);
      sftpHandler.downloadFile(credentials.sftpPathToImportFiles+configFile.importFileName, callback);
                },
    function(callback) {
      // Change file encoding to UTF8
      util.log("Going to change file name of the file on SFTP prepending in_progress.");
      sftpHandler.renameFileOnSftp();
    },
    function(callback) {
      // Change file encoding to UTF8
      util.log("Going to change file encoding to UTF8.");
      readFileSync_encoding(configFile.importFileName, "ISO-8859-1", callback);
    },
    function(fileData, callback) {
      util.log("Parsing the file data.");
        parseFileData(fileData, configFile, callback);
    }],
    // the bonus final callback function
    function(err, status) {
      if (err) {
        // app has crashed
        util.log(err);
        return;
      }
      // keep looping with 1 min delay
      util.log(status);
      setTimeout(function() {
        mainProcess();
      }, 60000);
      return;
    });
 }

瀑布功能:

downloadFile

SftpHandler.prototype.downloadFile = function (path, callback) { // Download swush file from SFTP client.scp({ 'host': this.host, 'username': this.username, 'password': this.password, 'path': path },'./', function(err) { if (err) { return callback("File: "+path+" "+err); } else { return callback(null); } }); }; 函数:

29 Jul 11:58:01 - Main process started.
29 Jul 11:58:01 - Running through all config files. Count: 3
29 Jul 11:58:01 - Downloading config files.0
29 Jul 11:58:01 - Downloading config files.1
29 Jul 11:58:01 - Downloading config files.2
29 Jul 11:58:02 - File: /E-drive/sftp/VismaReports/Test/QueueSystem/test2.csv Er
ror: file does not exist
29 Jul 11:58:03 - File: /E-drive/sftp/VismaReports/Test/QueueSystem/test5.csv Er
ror: file does not exist
29 Jul 11:58:03 - Going to change file name of the file on SFTP prepending in_pr
ogress.

输出:

Downloading config files.+i

在实际下载发生之前,我不希望看到{{1}}在那里看到3次。在我看来,它是异步的,我不希望它出现。

1 个答案:

答案 0 :(得分:0)

for (var i=0; i<configFiles.length;i++){
        waterfallMain(configFiles[i],i);
}

您将异步代码作为同步运行。试试如下

async.eachSeries(configFiles, waterfallMain, function(err) { // sequence run 
    console.log((err) ? err.message : 'Done')}; 

    // Process error here
})
...
function waterfallMain(configFile, callback){
    async.waterfall([
            function(callback){
                util.log("Downloading config files.", configFiles);
                sftpHandler.downloadFile(credentials.sftpPathToImportFiles+configFile.importFileName, callback);
            },
            function(callback) {
                util.log("Going to change file name of the file on SFTP prepending in_progress.");
                sftpHandler.renameFileOnSftp(callback); // !!! You must call callback function to move next 
            },
            function(callback) {
                util.log("Going to change file encoding to UTF8.");
                readFileSync_encoding(configFile.importFileName, "ISO-8859-1", callback); // !!! Smth wrong readFileSync_encoding like sync-function. Why it call callback? 
            },
            function(fileData, callback) {
                util.log("Parsing the file data.");
                parseFileData(fileData, configFile, callback);
            }],
            callback // !!! simple call callback. Errors processing above.
/* This function is not good
        // the bonus final callback function
        function(err, status) {  // !!! what is status? Where it's set?
            // app has crashed
            if (err) 
                return util.log(err); // !!! shortly

            // !!! WTF?!
            // keep looping with 1 min delay
            util.log(status);
            setTimeout(function(){
                mainProcess();
            }, 60000);
        }
*/      
        );
}