Node.js Async在使用while或forever时无法正常工作

时间:2015-07-16 12:37:42

标签: javascript node.js async.js

我在数据库中有数据,我想积累。因此,我需要一个被称为reapeatedly几次而不是并行的函数。它应该等待功能才能完成。 我曾经遇到异步并试图多次调用该函数,但循环迭代只发生一次......

function callDocAccumulation() {
    var db = openSQL();
    db.serialize(function() {
        db.get(batchSQL.queryProgramLineCount, function(err, row) {
            var count = 0;
            console.log('count rows ' + row.size);
            var async = require('async');
            async.whilst(function() {
                return count < row.size
            }, function(next) {
                docAccumulation();
                console.log('counter whilst ' + count)
                count++;
                /*added function*/
                setTimeout(next, 1000); 
            }, function(err) {
                console.log(err);
            });
        });
    });
    db.close();
}

function callDocAccumulation2() {
    console.log('callDocAccumulation2')
    var async = require('async');
    var err = function(err) {
        console.log(err);
    };
    var call = function() {
        console.log('first call of docAccumulation')
        docAccumulation();
    };
    async.forever(call, err);
}
docAccumulation有点复杂。我正在检查是否存在尚未处理的行。然后我看看我是否可以累积,因为在另一个表中存在一个条目,否则它是积累的开始。

function docAccumulation(){
    var db = openSQL();
    db.serialize(function() {
        db.get(batchSQL.queryProgramLine, function(err, row) {
            var idProgram = row.idProgram;
            var Name = row.Name;
            var ProgramName = row.ProgramName;
            var ProgramPath = row.ProgramPath;
            var DocumentPath = row.DocumentPath;
            var StartDate = new Date(row.StartDate);
            var EndDate = new Date(row.EndDate);
            var seconds = (EndDate.getTime() - StartDate.getTime())/1000;
            console.log(seconds);
            console.log(batchSQL.queryProcessedLine(row.Name,row.ProgramName,row.DocumentPath));
            db.get(batchSQL.queryProcessedLine(Name,ProgramName,DocumentPath), function(err, row) {
                if(row === undefined || row === null){
                    var stmt = db.prepare(batchSQL.insertRowProc);
                    stmt.run([Name, ProgramName, ProgramPath, DocumentPath, seconds], function(error){
                        console.log('lastInsert ' + this.lastID);
                        var stmt = db.prepare(batchSQL.updateRowProgram);
                        stmt.run(this.lastID, idProgram);
                        stmt.finalize();
                    }); 
                    stmt.finalize();
                }else{
                    console.log('seconds before ' + seconds);
                    console.log('seconds row.time ' + row.Time);
                    seconds += row.Time;//increment time
                    console.log('seconds after ' + seconds);
                    var stmt = db.prepare(batchSQL.updateRowProcessed);
                    stmt.run([seconds, row.idProcessed], function(error){
                        console.log('lastInsert ' + row.idProcessed);
                        var stmt = db.prepare(batchSQL.updateRowProgram);
                        stmt.run(row.idProcessed, idProgram);
                        stmt.finalize();
                    }); 
                    stmt.finalize();
                }
            });
        });
});
}  

我的错误在哪里?无论我调用什么函数,循环只调用一次。从日志我可以看出,行数更高。 递归会变得更聪明吗?

0 个答案:

没有答案