我有一個異步函數,說 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);
});
你能更好地格式化你的代碼嗎? – simonzack