在NodeJS中使用Async做什么

时间:2016-01-26 14:05:42

标签: javascript node.js

我遇到异步问题。 来自pic的此代码的结果应为:

First:0
Second:2
Item ADD!
First:1
Second:2
Item ADD!
First:2
Second:2
Item ADD!

这里的脚本应该停止。

我知道代码很长但我不能减少代码。

connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid  FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.`status` = 1').then(function(rows){
                    rows.forEach(function(record) {
                        var offer = manager.createOffer('76512333220');  
                        inventory.forEach(function(item) {
                            connection.query('SELECT amount_two FROM transaction where id = \'' + record.tid + '\'').then(function(w){
                                console.log("First: " + w[0].amount_two);
                                console.log("Second: " + record.amount);
                                if(w[0].amount_two <= record.amount)
                                {
                                    if(item.market_hash_name == record.real_name)
                                    {
                                        var asid = item.assetid;
                                        connection.query('SELECT count(id) as wynik FROM used where asset_id = \'' + asid + '\'').then(function(wiersze){
                                            if (wiersze[0].wynik == 0) 
                                            {
                                                var employee = {
                                                        asset_id: asid,
                                                        trans_id: record.tid
                                                    };
                                                 connection.query('INSERT INTO used SET ?', employee).then(function(rows){
                                                    offer.addMyItem(item);
                                                    console.log('Item ADD!');
                                                    connection.query('UPDATE `transaction` SET `amount_two`= `amount_two` + 1 WHERE `id`=\''+record.tid+'\'').then(function(rows){

                                                    });
                                                });   

                                            }   
                                        });
                                    }   
                                }           

                            });
                        });
                    });
                });

enter image description here

1 个答案:

答案 0 :(得分:0)

你有一个forEach你做异步的东西(做查询)。当db返回结果时,将调用db查询的回调结果。但到那时,你已经将数组循环到最后。 这就是节点和JavaScript从根本上起作用的方式。

如果您想要执行此类异步操作,请查看async.eachasync.parallel文档。它在循环函数参数中提供回调,告诉代码迭代完成。然后将最终结果收集到async.each的3:rd参数,这是一个在所有迭代完成后将运行的函数。

更新

您可以考虑加入两个选择查询,以免对数据库发出太多请求。这也会带来更好的表现。

代码示例:

connection.query(selectQuery).then(function (rows) {
    async.each(rows, function (record, next) {
        // perform the logic of each record!
        // Get or set data to db, calculate things etc. Ex:
        connection.query(saveQuery).then(function (err, res) {
            // this will tell async you are done with the record
            next(err, res);
        });
    }, function (err, result) {
        // here is error if any iteration went south,
        // or the final result: an array with what ever was passed into
        // the next function while iterating.
        // Pass results to where you want it. You can log it here if you want.
    });
});