Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js async.瀑布在函数完成之前跳到结尾_Node.js_Asynchronous_Underscore.js - Fatal编程技术网

Node.js async.瀑布在函数完成之前跳到结尾

Node.js async.瀑布在函数完成之前跳到结尾,node.js,asynchronous,underscore.js,Node.js,Asynchronous,Underscore.js,希望你们都好? 我正在潜入Node.js,不得不重新学习我过去编写代码的许多方法,并且不得不重新训练自己使用异步方法。。。我正在编写一个类似于JavaPOJO的服务器端脚本,它可以从命令行运行,也可以从事件触发 我想让一个函数的outputreturn作为下一个函数的输入,所以我决定使用async.瀑布-当我读到这篇文章时,将按顺序执行函数,使用一个函数的输出作为另一个函数的输入 脚本的思想是遍历给定的文件夹结构,创建子文件夹数组,然后将该数组传递给下一个文件夹。然后对该数组中的每个路径执行相同

希望你们都好? 我正在潜入Node.js,不得不重新学习我过去编写代码的许多方法,并且不得不重新训练自己使用异步方法。。。我正在编写一个类似于JavaPOJO的服务器端脚本,它可以从命令行运行,也可以从事件触发

我想让一个函数的outputreturn作为下一个函数的输入,所以我决定使用async.瀑布-当我读到这篇文章时,将按顺序执行函数,使用一个函数的输出作为另一个函数的输入

脚本的思想是遍历给定的文件夹结构,创建子文件夹数组,然后将该数组传递给下一个文件夹。然后对该数组中的每个路径执行相同的操作。我想使用underline.js和z.each函数,因为这似乎是按顺序遍历数组的好方法。但这正是我被卡住的地方,因为它似乎在工作完成之前就完成了所有的功能。所以我的逻辑有点偏离了

我使用“漫游”功能进入文件夹并返回所有子文件夹。。这个想法是脚本将运行,然后在瀑布的末尾“process.exit”

代码是:



    async.waterfall([
      function(callback){ /* Get List of Artists from MusicFolder */
        console.log('first->IN');
        walk(musicFolder, function(err, foldersFound) {
          if (err) { return err;}
          _.each(foldersFound, function(folderPath){
            console.log('Folder: ' + folderPath);
          });
          console.log('first->OUT');
          callback(null, foldersFound);
        });
      },
      function(artistsFound, callback){ /* Get List of Albums from Artist Folders */
        var eachLoop=null;
        console.log('second->IN');
        _.each(artistsFound, function(artistPath){
          console.log('second->eachPath:Start:'+artistPath);
          walk(artistPath, function(err, albumsFound) {
            console.log('second->Walk:Found');
            console.log(albumsFound);
            if (err) { console.log(err);}
            _.each(albumsFound, function(albumPath){
              eachLoop++;
              console.log('second->Walk:each:'+eachLoop);
            });
            console.log('second->Walk:End');
          });
          console.log('second->eachPath:End:'+artistPath);
        });
        console.log('second->OUT');
        callback(null, albumsFound);
      },
      function(paths, callback){
        console.log('third->IN');
        console.log('third->OUT');
        callback(null, paths);
      }
    ], function (err, result) {
        console.log('last->IN');
        console.log(result);
        console.log('last->OUT');
    //    process.exit();
    });

我已经注释掉了示例中的“process.exit”

如果取消对“process.exit”的注释,则会得到以下输出:

first->IN
Folder: /music/Adele
Folder: /music/Alex Clare
first->OUT
second->IN
second->eachPath:Start:/music/Adele
second->eachPath:End:/music/Adele
second->eachPath:Start:/music/Alex Clare
second->eachPath:End:/music/Alex Clare
second->OUT
third->IN
third->OUT
last->IN
null
last->OUT
我能看到的是,它没有在第二个瀑布函数中输入“walk”函数,而是完全跳过了“walk”,即使“walk”在每次迭代中都在u.内

如果在最后一个函数中注释掉“process.exit”命令,则会得到以下结果:

first->IN
Folder: /music/Adele
Folder: /music/Alex Clare
first->OUT
second->IN
second->eachPath:Start:/music/Adele
second->eachPath:End:/music/Adele
second->eachPath:Start:/music/Alex Clare
second->eachPath:End:/music/Alex Clare
second->OUT
third->IN
third->OUT
last->IN
null
last->OUT
second->Walk:Found
[ '/music/Alex Clare/The Lateness of the Hour' ]
second->Walk:each:1
second->Walk:End
second->Walk:Found
[ '/music/Adele/19',
  '/music/Adele/21',
  '/music/Adele/Live At The Royal Albert Hall' ]
second->Walk:each:2
second->Walk:each:3
second->Walk:each:4
second->Walk:End
我承认这是令人沮丧的。任何帮助都将不胜感激,因为在过去的一周里,我一直在用各种“异步”形式一遍又一遍地重写它,它们都过早地跳出了函数,所以一切都是无序的

提前感谢您的帮助或想法:

标记

您的行走功能似乎是异步的。您希望启动并行异步作业,合并结果并沿着瀑布向下移动。因此,您可以做的是将async.瀑布与async.parallel结合起来。您的第二个函数可能如下所示:

function(artistsFound, callback) {
    // some code
    var jobs = [];
    artistsFound.forEach(function(artistPath) {
        jobs.push(function(clb) {
            walk(artistPath, function(err, albumsFound) {
                // some code
                clb(err, albumsFound);
            });
        });
    });
    // some code
    async.parallel(jobs, callback);
}

旁注:您不必使用下划线.js来简单地循环数组。现代JavaScript有一个内置的.forEach函数。

Walk函数不是异步的,它只在遍历所有文件夹并将它们添加到数组中后返回,一旦完成,数组就会传递。-我想把它贴在这里,但不允许,因为我不能用“答案”回复。所以不确定你的async.parallel选项是否有效-实际上已经尝试过了,公平性方面它和我的一样…:感谢有一个javascript.forEach,但我也在自学下划线,所以我认为养成使用它的习惯会很好。@Pandafinity如果walk函数是同步的,那么我不相信代码会像你说的那样:无论如何,我没有主意了,所以我希望其他人能解决你的问题。祝你好运感谢您的想法和建议:…这是一个标准的“walk”,完成后只返回一个字符串数组..因此您只返回['subfolder path 1'、'sub folder path 2',…]类型的数组。希望你所在的地方阳光明媚。。