2016-04-14 5 views
0

アイデアは、ジオコードを複数の時間(配列に対して)実行することです。非同期関数をループするために、私は再帰方法を使うことにしました。非同期ループの再帰を使用する:プッシュアレイデータへのアクセス方法?

var geocoder = require('geocoder') 
var geocoded = [] 

//Example array 
var result = [{ 
    'company_no': 'A', 
    'address': 'a' 
}, { 
    'company_no ': 'B', 
    'address': 'b' 
}] 

function geocodeOneAsync(result, callback) { 
    var n = result.length 

    function tryNextGeocode(i) { 
    if(i >= n) { 
     //onfailure("alldownloadfailed") 
     return 
    } 
    var address = result[i].address 
    geocoder.geocode(address, function (err, data) { 

     geocoded.push(result[i].company_no) 
     console.log('data1' + JSON.stringify(
      geocoded)) //Result is ==> data1["A"], data1["B"] 
     tryNextGeocode(i + 1) 

     // } 
    }) 
    } 
    console.log('data1' + JSON.stringify(geocoded)) 
    tryNextGeocode(0) 
} 
geocodeOneAsync(result, function() { 
    JSON.stringify('data final ' + geocoded) // result is empty []. I want to access the final geocoded array? 

}) 

基本的にどのように私は最終的な価値を得ることができますか?

+0

トップでそのオブジェクトと一体何が起こっているのですか?あなたの質問のコードの書式を改善する必要があります。 –

+0

標準のインデントを使用してコードを読み取り可能にしてください。 http://jsbeautifier.org/またはIDEが役立ちます。 – Bergi

+0

どこでも 'callback'を呼び出すことはありませんか?ベースケースでそのまま使用してください。 – Bergi

答えて

0

あなたの質問に答えられない場合は、お詫び申し上げます。

if (i >= n) { 
    callback(); 
} 

完全なコード(私は自分自身のためにそれを修正)::私が手に出力され

var geocoder = require('geocoder'); 
var geocoded = []; 

function geocodeOneAsync(result, callback) { 
    var n = result.length; 
    function tryNextGeocode(ii) { 
     if (ii >= n) { 
      //onfailure("alldownloadfailed") 
      callback(); 
      return; 
     } 

     var address = result[ii].address 
     geocoder.geocode(address, function (err, data) { 
      geocoded.push(result[ii].company_no); 
      console.log('data1' + JSON.stringify(geocoded)); //Result is ==> data1["A"], data1["B"]_++ 
      console.log("n=" +n + ",ii=" + ii); 
      tryNextGeocode(ii + 1); 
     }); 
    } 
    console.log('data1' + JSON.stringify(geocoded)); 
    tryNextGeocode(0); 
}; 

//Example array 
var result = [ 
    {'company_no': 'A,','address': 'a'}, 
    {'company_no': 'B', 'address': 'b'} 
]; 
geocodeOneAsync(result, function() { 
    console.log(JSON.stringify('data final ' + geocoded)); // result is empty []. I want to access the final geocoded array? 
}); 

data1[] 
data1["A,"] 
n=2,ii=0 
data1["A,","B"] 
n=2,ii=1 
"data final A,,B" 
私はあなたが再帰終了条件にコールバックを呼び出すために必要と考えています

希望に役立ちます!

+0

ありがとうWestandy ... look gr8 – FRizal

1

これは、再帰ではなくmapとPromiseを使うのが最も簡単です。

function geocodeOneAsync(result, callback) { 
    // with map you get an array of promises 
    var promises = result.map(function (company) { 
     return new Promise(function (resolve, reject) { 
      var address = company.address; 
      geocoder.geocode(address, function (err, data) { 
       if(err) { 
        reject(err); 
       } 
       resolve(company.company_no); 
      }); 
     }).catch(function(error) { 
      // you can handle error here if you don't want the first occuring error to abort the operation. 
     }); 
    }); 

    // then you resolve the promises passing the array of result to the callback. 
    Promise.all(promises).then(callback); 
} 

geocodeOneAsync(result, function (geocodedArray) { 
    // here geocoded is ['A','B'] 
    JSON.stringify(geocodedArray); 
}); 

追加のボーナスとして、すべての非同期操作が並行して行われます。

+0

うわー、これは最高です... thx。 – FRizal

+0

opps ..コードを試しました。TypeError:result.map(...)でエラーが見つかりました。catchは関数ではありません – FRizal

+0

Oups Typo。キャッチは新しいPromise not mapにする必要があります。 –

関連する問題