Node.js的:你什么时候知道什么时候异步任务采集完成? [英] Node.js: When do you know when a collection of asynchronous tasks is done?

查看:119
本文介绍了Node.js的:你什么时候知道什么时候异步任务采集完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下一个目录,并检查每个文件我看到有一个正则的前pression的名称。基本上,常见的unix 的一个版本,找到命令,只写在Node.js的我不关心文件的顺序,但我要确保我得到的所有的人。

我有以下的code,这是接近(我认为)我想要的东西。它需要一个STARTDIR,一个正则表达式和回调;每个文件分析了它加一定点的值,当它与分析做了递减的哨兵。我担心的是,如果有一个文件和目录的深层嵌套集合,它会分析该文件,并触发长回调找到第二个文件之前,和可能的回调会被调用两次。

显然,我可以prevent被调用的回调被具有解雇变量从发射第二次克制它的两倍。但是,仍然会给我错误的数据。我在做什么错在这里,而且是有多个节点,合适的方式来做到这一点?

  FS =要求(FS)
PATH =要求(路径)功能get_all_files(STARTDIR,正则表达式,回调){
    VAR定点= 0;
    变种结果= [];    功能check_sentinel(){
        哨兵 - ;
        如果(定点=== 0){
            回调(结果);
        }
    }    功能check_file(目录,文件名){
        VAR FNAME;
        定点++;
        如果(regexp.test(文件名)){
            results.push(path.join(目录,文件名));
        }
        check_sentinel();
    }    功能check_directory(DIR){
        fs.readdir(path.join(this.rootpath,dirpath),功能(ERR文件){
            VAR FNAME,我,LEN,将npath;
            如果(ERR){
                扔犯错
            }            对于(i = 0,LEN = files.length; I< LEN,我++){
                FNAME =文件[I]
                将npath = path.join(DIR,FNAME);
                fs.stat(将npath,函数(ERR,统计数据){
                    如果(stats.isFile()){
                        check_file(DIR,FNAME);
                    }其他{
                        如果(stats.isDirectory()){
                            check_directory(将npath);
                        }
                    }
                });
            }
        });
    }
    check_directory(STARTDIR);
}


解决方案

一对夫妇的想法......

我从来没有使用它,但最简单的方法做你所问的可能是使用异步.js文件 walkfiles功能。见一个例子测试这里

否则,我会考虑建筑的函数调用数组,并从你的递归目录行走功能返回数组(而不是使用定点等)。换句话说,check_directory返回的函数调用匹配您正在寻找的文件的数组。如果没有文件,则数组为空。

最后,在递归的顶部结合阵列和使用异步的库(不一样作为async.js)来执行的一次全部使用平行功能(<一个函数的数组href=\"http://stackoverflow.com/questions/5172244/idiomatic-way-to-wait-for-multiple-callbacks-in-node-js\">see这个线程的例子使用系列)。

I would like to descend a directory, and examine the name of every file I see there against a regular expression. Basically, a version of the common unix find command, only written in Node.js. I don't care about the order of the files, but I do want to make sure I get all of them.

I have the following code, which is close (I think) to what I want. It takes a startdir, a regexp, and a callback; for each file it analyzes it increments the value of sentinel by one, and when it is done with the analysis it decrements the sentinel. My concern is that if there's one file, and a deeply nested collection of directories, it will analyze that file and trigger the callback long before it finds a second file, and possible the callback will be called twice.

Obviously, I could prevent the callback from being called twice by having a fired variable to restrain it from firing a second time. But that still would give me erroneous data. What am I doing wrong here, and is there a more node-appropriate way to do it?

fs = require('fs')
path = require('path')

function get_all_files(startdir, regexp, callback) {
    var sentinel = 0;
    var results = [];

    function check_sentinel() {
        sentinel--;
        if (sentinel === 0) {
            callback(results);
        }
    }

    function check_file(dir, filename) {
        var fname;
        sentinel++;
        if (regexp.test(filename)) {
            results.push(path.join(dir, filename));
        }
        check_sentinel();
    }

    function check_directory(dir) {
        fs.readdir(path.join(this.rootpath, dirpath), function(err, files) {
            var fname, i, len, npath;
            if (err) {
                throw err
            }

            for (i = 0, len = files.length; i < len; i++) {
                fname = files[i];
                npath = path.join(dir, fname);
                fs.stat(npath, function(err, stats) {
                    if (stats.isFile()) {
                        check_file(dir, fname);
                    } else {
                        if (stats.isDirectory()) {
                            check_directory(npath);
                        }
                    }
                });
            }
        });
    }
    check_directory(startdir);
}

解决方案

A couple of thoughts...

I have never used it, but the simplest way to do what you are asking might be to use the async.js walkfiles function. See the tests for an example here.

Otherwise, I would consider building an array of function calls and returning the array from your recursive directory walking function (rather than using a sentinel, etc). In other words, check_directory returns an array of function calls matching the files you are looking for. If there is no file, the array is empty.

Finally, combine the array at the top of the recursion and use the async library (not the same as async.js) to execute the array of functions all at once using the parallel function (see this thread for an example using "series").

这篇关于Node.js的:你什么时候知道什么时候异步任务采集完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆