如何在节点js中打破内部异步循环

时间:2016-03-04 09:24:29

标签: javascript node.js asynchronous

请看这个代码为什么内循环没有破坏。 我想只在内循环条件满足时才推入新数组 这应该打破内部循环的迭代,但外部循环应该像往常一样迭代。

Inspired from

这是我的代码:

var rows = [ RowDataPacket {
                                    id: 1,
                                    title: 'this is jg case',
                                    description: 'this is reply to jg case',
                                    type: 2,
                                    created: Thu Mar 03 2016 12:50:20 GMT+0530 (IST),
                                    group_id: '1,8',
                                    AOI: '1,8' },
                                  RowDataPacket {
                                    id: 2,
                                    title: 'this is jg case',
                                    description: 'this is another reply to jg case',
                                    type: 1,
                                    created: Thu Mar 03 2016 12:55:01 GMT+0530 (IST),
                                    group_id: '3,9',
                                    AOI: '1,8' },
                                  RowDataPacket {
                                    id: 3,
                                    title: 'this is jg case',
                                    description: 'this is again another reply to jg case',
                                    type: 1,
                                    created: Thu Mar 03 2016 12:58:01 GMT+0530 (IST),
                                    group_id: '3,8',
                                    AOI: '1,8' }                                      
                                ];


                    var filteredRows = [];

                    async.each(rows,  function(row, callback1){
                        var eachRow = row.group_id.split(',').map(Number);
                        async.each(eachRow, function(group, callback2){

                            if(AOI.indexOf(group) !== -1){
                                filteredRows.push(row);
                                console.log(group,row);
                                var fakeErr = new Error();
                                fakeErr.break = true;
                                return callback2(fakeErr);
                            }

                            callback2();

                        },  function(err) {

                                if (err && err.break){
                                  console.log("should break the inner loop and continue for the next outer loop")
                                  callback1(); 
                                }
                                else
                                  callback1(err); 
                            });
                    },  function(err) {

                          if (err)
                            callback(err, null);

                          callback(null, filteredRows);
                    });

预期产出:

var filteredRows = [ RowDataPacket {
                                id: 1,
                                title: 'this is jg case',
                                description: 'this is reply to jg case',
                                type: 2,
                                created: Thu Mar 03 2016 12:50:20 GMT+0530 (IST),
                                group_id: '1,8',
                                AOI: '1,8' },
                              RowDataPacket {
                                id: 3,
                                title: 'this is jg case',
                                description: 'this is again another reply to jg case',
                                type: 1,
                                created: Thu Mar 03 2016 12:58:01 GMT+0530 (IST),
                                group_id: '3,8',
                                AOI: '1,8' }                                      
                            ];

2 个答案:

答案 0 :(得分:0)

而不是你的代码:

return callback2(fakeErr);

您更改为:

callback2(fakeErr);
return;

你应该回到这里,如果没有,你的程序会收到两个回调

if (err)
    callback(err, null);
    // return here
    return;

callback(null, filteredRows);

答案 1 :(得分:-1)

each()并行运行迭代,你不能打破循环" (当你这样做,下一次迭代已经在运行),你必须使用eachSeries()来控制异步流程:

// process rows in parallel
async.each(rows, function(row, callback1) {
  var eachRow = row.group_id.split(',').map(Number);
  // for each row, process each group one at a time so you can break the loop
  async.eachSeries(eachRow, function(group, callback2) {
    // stuff
  });
  // stuff
});