javascript - variable scope in module asynchronous function -


this first week in node i'm sorry if no brainier.

the code works , should. can't figure out how match name (url) starts http.get whit result gets website.

i found witch problem, except premade function can't edit function , add callback.

variable scope in asynchronous function

if run code synchronous or make callback in http.get function good. don't have skills , don't know if can it.

thanks - robin.

http = require('http'); function download(name) { //name array whit csgo items names. (var = 0; < name.length; i++) {      var markethashname = getgoodname(name[i]);     var url = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' + markethashname;      http.get(url, function (res) {         var data = "";         res.on('data', function (chunk) {             data += chunk;         });         res.on("end", function () {              data = json.parse(data);             var value= 0;             //get value in json array             if(data.median_price) {                 value = data.median_price;             }else{                 value = data.lowest_price;             }              value = value.substr(5);             console.log("weapon",value);             //callback whit name/link , value?             //callback(name,value);         });     }).on("error", function () {      }); } 

}

you can add callback argument , call final data. and, if want pass callback particular markethashname being processed, can create closure capture uniquely each time through for loop:

http = require('http');  function download(name, callback) {     //name array whit csgo items names.     (var = 0; < name.length; i++) {          var markethashname = getgoodname(name[i]);         // create closure capture markethashname uniquely each          // iteration of loop         (function(thename) {             var url = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' + markethashname;              http.get(url, function (res) {                 var data = "";                 res.on('data', function (chunk) {                     data += chunk;                 });                 res.on("end", function () {                      data = json.parse(data);                     var value= 0;                     //get value in json array                     if(data.median_price) {                         value = data.median_price;                     }else{                         value = data.lowest_price;                     }                      value = value.substr(5);                     console.log("weapon",value);                      // async function done, call callback                     // , pass our results                     callback(thename, value, data);                 });             }).on("error", function () {              });         })(markethasname);     } }  // sample usage: download("whatever", function(name, value, data) {    // put code here use results }); 

fyi, may find request module higher level set of functionality on top of http module save work.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -