2013-11-24 51 views
0

我有一個異步函數,說 getConfigPricingFromResourceQuery(元件,功能(configPricingFromResourceQuery){如何調用異步函數每次在陣列PARAM每個對象

//做的東西 });

我發送元素[]數組的每個值到這個異步函數。但是這個函數甚至被稱爲單一時間 - 儘管該數組包含多個值。更清楚的是,這個函數被稱爲單次發送所有的值到這個函數。但這並不是我們期望的,因爲我需要爲數組參數的每個值做些事情。

功能確定指標:

功能getConfigPricingFromResourceQuery(元素,回調){

  var configJson = {}; 

      // configJson contains parameters required for resourceservice validate api. 
      var configJson = elements.parameters; 

      trace.info(elements.uri); 

      configJson.instanceTypeUri = elements.uri; 

      jsonObject = JSON.stringify(configJson); 

       // prepare the header 
      var postheaders = { 
      'Content-Type' : 'application/json', 
      'Content-Length' : Buffer.byteLength(jsonObject, 'utf8') 
      }; 

      // the post options 
      var optionspost = { 
      host : '192.168.3.243', 
      port : 5902, 
      path : '/apiv1.0/resourceQuery/validate', 
      method : 'POST', 
      headers : postheaders 
      }; 

      // do the POST call to resource service 
      var reqPost = http.get(optionspost, function(response) { 
       trace.log("statusCode: ", response.statusCode); 

       response.on('data', function(d) { 
        trace.info('POST result:\n'); 
        process.stdout.write(d); 
        configPricing += d; 
        trace.info(configPricing); 
        trace.info('POST completed'); 
        return callback(JSON.parse(configPricing)); 
       }); 
      }); 
      // write the json data 
      reqPost.write(jsonObject); 
      //reqPost.end(); 
      reqPost.on('error', function(e) { 
       console.error(e); 
      }); 

     //return callback(configPricing); 
    }; 

調用此函數:

完整代碼下面給出

getConfigPricingFromResourceQuery(元素,功能( configPricingFromResourceQuery){ trace.info('########################### #############'); var metadataConfigJsonArray = []; VAR metadataConfigJson = {

    chargeAmount: Math.round(configPricingFromResourceQuery.totalUnitPrice * 100)/100, 
        chargeamountUnit: configPricingFromResourceQuery.ChargeAmountUnit, 
        currencyCode: configPricingFromResourceQuery.CurrencyCode 
       } 
      metadataConfigJsonArray.push(metadataConfigJson); 
      if(elements.metadata) { } 
      else 
      { 
       elements.metadata={}; 
      } 

      metadataModified = elements.metadata; 

      // TODO : Remove this hard-coding 
      elementlevelpricingSummary.Name = 'Hardware'; 

      if(configPricingFromResourceQuery.totalUnitPrice) 
      { 
       var chargeAmount = configPricingFromResourceQuery.totalUnitPrice; 

       elementlevelpricingSummary.chargeAmount = Math.round(chargeAmount * 100)/100; 
      } 
      if(configPricingFromResourceQuery.ChargeAmountUnit) 
      { 
       var chargeAmountUnit = configPricingFromResourceQuery.ChargeAmountUnit; 

       elementlevelpricingSummary.chargeAmountUnit = configPricingFromResourceQuery.ChargeAmountUnit; 
      } 
      if(configPricingFromResourceQuery.CurrencyCode) 
      { 
       var currencyCode = configPricingFromResourceQuery.CurrencyCode; 

       elementlevelpricingSummary.currencyCode = configPricingFromResourceQuery.CurrencyCode; 
      } 

      metadataModified.pricingSummary = metadataConfigJsonArray; 

      configurableElementarray.push(elementlevelpricingSummary); 

      // delete original metadata from workload json (to be replaced by metadata containing pricing summary) 
      delete elementinfo.metadata; 

      elementinfo.metadata = metadataModified; 

      elementArray.push(elementinfo); 

      // global workloadinfo variable is appended with array of elements with its pricing summary within metadata of respective elements 
      workloadinfo.elements = elementArray; 

      return callback(null, workloadinfo); 
      }); 
+1

你能更好地格式化你的代碼嗎? – simonzack

回答

0

這就是所謂的一次,因爲你只把它稱爲一次,傳遞整個數組中:

getConfigPricingFromResourceQuery(elements, function(result){ 
    // Your lovely callback 
}) 

如果你想獲得的配置價格爲元素陣列中的每個對象然後你要叫它爲他們每個人的

elements.forEach(function (e) { 
    getConfigPricingFromResourceQuery(e, function (result) { 
     // handle the result 
    } 
}) 

我也建議你使用節點樣式的回調function (err, value) {},讓您考績通過回調的錯誤。

如果你想把你的元素數組變成一個結果數組,那麼你需要更聰明些,你應該使用一個模塊來爲你做。在npm上尋找一個並行陣列模塊,如continuable-para

var para = require("continuable-para") 

para(elements.map(function() { 
    return getConfigPricingFromResourceQuery.bind(null, e) 
}), function (err, pricesArray) { 
    if (err) { 
     return console.log(err) 
    } 

    pricesArray.forEach(function (price) { /* handle the price */ }) 
}) 
相關問題