2016-04-29 15 views
1

ノードJavaScriptで作業しており、過去のループが完了したときに新しい関数を実行しようとしています。以下のコードでは、// loop through objects in data, to process itを使用して、データを基にしたforループを表します。ここで、データは配列に追加されます。操作が完了したときに関数を実行する

各応答では、zが1増加します。しかし、データが時々処理される前にzが40に達することがよくあります。ループ内にzを置くことはできません。各ページ内には多くのオブジェクトがあるためです。

誰もが、ループが完了した後にzを増やし、それをチェックする方法を提案できますか?

var req = http.request(headers, function(response) { 
    response.on('error', function (e) { 
     // 
    }) 
    response.on('data', function (chunk) { 
     // 
    }); 
    response.on('end', function() { 
     // loop through objects in data, to process it 
     z++ 
     if (z == 40) { 
      newFunction(values, totals) 
     }; 
    }); 
}) 
req.on('error', function(e) { 
    // 
}) 
req.end(); 
+1

「誰もがZのみ増加し、ループが完了した後に、それが40に等しいチェックする方法を提案してもらえますか?」これはあなたのループがあなたのコメントの場所であると仮定して、あなたがすでにしていることです。ループがいくつかの非同期タスクをトリガする場合、完了したタスクはループの完了とは関係ありません。 – Paulpro

+1

より具体的な詳細を教えてください。 * "データが処理されました" *:どのデータですか?どのコードですか?データが処理されているかどうかに関係している場合は、関連するコードを入力することが不可欠です。 – trincot

+0

@trincot HTTPリクエストから受信したデータは処理されますが、これはしばしば "if(z == 40){}"条件がtrueを返した後です。データが配列に格納され/処理された後、 "if(z == 40){}"条件を実行します。 – GregW

答えて

1

私はtrincotに同意します、私は少し具体的にお願いします。 しかし、私が集めたものから、async.each()を使ってみることができます。

これが役立つことを願っています。

0

各応答のループ内で、処理されたオブジェクトの数を応答内のオブジェクトの総数と比較します。その数に達したときにのみ増分します。

loop through responses 
    loop through objects in single response 
    if (data.length === counter) { 
     z ++; 
    } 
    counter ++; 
    } 
    if (40 === z) { 
    newFunction(values, totals); 
    } 
} 
+0

Asyncはあなたの友人です:) – kevingilbert100

0

一般的な非同期モジュールからasync.timesSeriesを使用することをお勧めします。

リンク:https://github.com/caolan/async#times

サンプルコード:)

// Required NPM Modules 
var async = require('async') 

// Run Async Times Series Function 
// Will execute 40 times then make the final callback 
async.timesSeries(40, asyncOperation, asyncTimesCallback) 


/** 
* This is the primary operation that will run 40 times 
* @param {Number} n - Sort of like the index so if its running 40 times and its the last time its running its gonna be 39 or 40 not sure you may have to test that 
* @param {Function} next - Callback function when operation is complete see async times docs or example in this code 
*/ 
function asyncOperation (n, next){ 

    var req = http.request(headers, function(response) { 
     response.on('error', function (e) { 
      return next(e, null) 
     }) 
     response.on('data', function (chunk) { 
      // 
     }) 
     response.on('end', function() { 
      // loop through objects in data, to process it 
      return next(null, 'some value?') 
     }) 
    }) 
    req.on('error', function(e) { 
     return next(e, null) 
    }) 
    req.end() 

} 


/** 
* This is run after the async operation is complete 
*/ 
function asyncTimesCallback (err, results) { 

    // Error Checking? 
    if(err) { 
     throw new Error(err) 
    } 

    // function to run wen operation is complete 
    newFunction(results['values'], results['totals']) 

} 


/** 
* New Function The Callback Function Calls 
*/ 
function newFunction(values, totals){ 

    console.log('Values', values) 
    console.log('Totals', totals) 

} 
関連する問題