使用nodejs,async,waterfall,each和mysql同步控制流

时间:2014-10-23 07:22:42

标签: node.js asynchronous

我正在使用与mysql异步的控制流问题,并希望得到一些帮助/建议。基本上,我有一系列的项目。我想遍历数组,并为每个项目检查它是否存在于MySQL数据库/表中。如果该项目不存在,请保存该项目,否则不要。所以整个用例是同步的。我所做的是在async.each中嵌套async.waterfall,因为我认为async.each将迭代数组中的每个项目,而瀑布将连续执行函数(使用作为参数传递的数据)对每个项目。但是,正在发生的是每个项目的每个函数都在执行,然后再次为每个项目执行下一个函数。不完全是我想要的。

无论如何,这里有一些伪代码:

var async = require('async');
// Assume that connection established to mysql db using mysql module
var testArray = [1,2,3,4];
var eachCounter = 0;
async.each(testArray, function(item,eachCallback){
    async.waterfall([
    function(callback){
        var formattedItem = item + ' some_formatting';
        console.log(item  + ' > ' + formattedItem);
        callback(null, formattedItem);
    },
    function(arg1, callback){
        // Using connection.query here to see if formattedItem exists in a table
        var mysqlRows = 0;
        if(mysqlRows === 0) {
            console.log(arg1  + ' does not exist so save it');
            var insertQuery = 'INSERT INTO ...';
            callback(null, insertQuery, arg1);
        }
        else if(mysqlRows > 0) {
            console.log(arg1  + ' does exist so don\'t save it');
            callback(null, null, arg1);
        }
    },
    function(arg1, arg2, callback){
        if(arg1 !== null) {
            console.log(arg2  + ' was inserted into mysql table');
            callback(null, 'done');
        }
        else {
            console.log(arg2  + ' was not inserted into mysql table');
            callback(null, 'done');
        }
    }
    ], function (err, result) {
       eachCounter++;
        if(eachCounter === testArray.length) {
             console.log('really done');
        }
    });
    eachCallback();
}, function(err){
    if(err) {
        console.log('failed');
    }
    else {
        console.log('success')
    }
});

结果是:

success
1 > 1 some_formatting
2 > 2 some_formatting
3 > 3 some_formatting
4 > 4 some_formatting
1 some_formatting does not exist so save it
2 some_formatting does not exist so save it
3 some_formatting does not exist so save it
4 some_formatting does not exist so save it
1 some_formatting was inserted into mysql table
2 some_formatting was inserted into mysql table
3 some_formatting was inserted into mysql table
4 some_formatting was inserted into mysql table
really done

当我真正想要的是:

success
1 > 1 some_formatting
1 some_formatting does not exist so save it
1 some_formatting was inserted into mysql table
2 > 2 some_formatting
2 some_formatting does not exist so save it
2 some_formatting was inserted into mysql table
3 > 3 some_formatting
3 some_formatting does not exist so save it
3 some_formatting was inserted into mysql table
4 > 4 some_formatting
4 some_formatting does not exist so save it
4 some_formatting was inserted into mysql table
really done

那么,我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

async.each并行完成。您希望使用async.eachSeries来使所有呼叫按顺序运行。