2016-10-11 14 views
1

nightmareコールバック関数の非同期を処理するために、asyncモジュールで入れ子のforループを実行しようとしています。簡単に言うと、私がやっているのは、同じのオブジェクトを同じnodeインスタンスで実行していて、Webサイト上の別のリンクに移動しているということです。node.jsの非同期モジュールをネストしたForループ

私が読んだことによると、ループに次のインデックスに移動するように通知するには、next()関数を呼び出す必要があります。これは、ただ一つのforループでうまくいきます。私がasyncOfSeriesを入れ子にしたとき、内部のnext()関数は、内側のループ上で、外側のループ上では実行されません。

あなたがよりよく理解できるように、コードのスニペットをご覧ください。

var Nightmare = require('nightmare'); 
var nightmare = Nightmare({show:true}) 

var complexObject = { 

19:["11","12","13","14","15"], 
21:["16"], 
22:["17"], 
23:["18","19"] 
}; 
//looping on each object property 
async.eachOfSeries(complexObject, function(item, keyDo, next){ 
    //here im looping on each key of the object, which are arrays 
    async.eachOfSeries(item, function(indexs, key, nexts){ 
    nightmare 
    .//do something 
    .then(function(body){ 

     nightmare 
     .//do something 
     .then(function(body2){ 

      nightmare 
      .//do something 
      .then(function(body3){ 

      //Here I call next() and expecting to call the next index of the inner loop 
      //but is calling the next index of the outer loop and the inner for is just 
      // executing one time. 
      next(); 
      }); 

     }); 

    }); 
}); 
}); 

を私は内側のループの後に別のnext()を呼び出すようにしようとしましたが、エラーを投げています。なぜ誰かが内部ループが1回だけ実行されているのか考えていますか?

+0

? –

+0

スニペットを編集しました。 @ stdob-- –

+3

あなたは約束を使っているので、おそらくasync.jsを使うべきではありません。彼らはうまく構成されません(あなたはエラーをまったく処理しません)。 – Bergi

答えて

0

管理する必要があるコールバックが2つあります。内側コールバック "nexts"と外側コールバック "next"。内側のループ内から外側のコールバックを呼び出しています。

これを試してください:あなたは変数 `nightmare`を宣言

var Nightmare = require('nightmare'); 
var nightmare = Nightmare({show:true}) 

var complexObject = { 

19:["11","12","13","14","15"], 
21:["16"], 
22:["17"], 
23:["18","19"] 
}; 
//looping on each object property 
async.eachOfSeries(complexObject, function(item, keyDo, next){ 
    //here im looping on each key of the object, which are arrays 
    async.eachOfSeries(item, function(indexs, key, nexts){ 
    nightmare 
    .//do something 
    .then(function(body){ 

     nightmare 
     .//do something 
     .then(function(body2){ 

      nightmare 
      .//do something 
      .then(function(body3){ 

      //Here I call next() and expecting to call the next index of the inner loop 
      //but is calling the next index of the outer loop and the inner for is just 
      // executing one time. 
      nexts(); 
      }); 

     }); 

    }); 
}); 
next(); 
}); 
+1

正当なのは、私は実際にここにコードを貼り付けていましたが、 next()と同じ名前の両方のコールバックを持っています。彼らは問題を引き起こしていたのと同じ範囲にいるので、私はこれを試してみます。ありがとう! –