【问题标题】:Sync control flow using nodejs, async, waterfall, each and mysql使用 nodejs、async、fallout、each 和 mysql 同步控制流
【发布时间】:2017-05-19 18:51:59
【问题描述】:

我在使用 async 和 mysql 时遇到了一些控制流问题,希望得到一些帮助/建议。基本上,我有一系列项目。我想遍历数组,并为每个项目检查它是否存在于 MySQL 数据库/表中。如果该项目不存在,则保存该项目,否则不存在。所以整个用例是同步的。我所做的是将 async.waterfall 嵌套在 async.each 中,因为我认为 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

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

【问题讨论】:

    标签: node.js asynchronous


    【解决方案1】:

    async.each 是并行完成的。您想使用async.eachSeries 使您的所有呼叫连续运行。

    【讨论】:

    • 感谢您的指点。不幸的是,切换到 eachSeries 并没有帮助。 eachSeries 迭代器中的瀑布函数仍然是并行执行而不是串行执行。
    • 哦,我明白了。将您的eachCallback 放入最终的瀑布函数(而不是瀑布数组)中。我想你还是会想要eachSeries。你想说,“当瀑布完成时,‘每个’都完成了”。
    • 啊——成功了!现在这完全有道理。谢谢!
    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2020-10-22
    • 2014-03-29
    • 2021-11-18
    • 2014-02-26
    相关资源
    最近更新 更多