【发布时间】:2013-11-24 21:38:51
【问题描述】:
我有一个异步函数,比如说 getConfigPricingFromResourceQuery(元素,函数(configPricingFromResourceQuery){
//做事 });
我将 elements[] 数组的每个值发送到这个异步函数。但是这个函数被调用一次,即使数组包含多个值。更清楚地说,这个函数被称为一次将所有值一起发送到这个函数。但这不是预期的,因为我需要为数组参数的每个值做一些事情。
完整代码如下:
函数定义:
function getConfigPricingFromResourceQuery(elements, callback) {
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);
});
【问题讨论】:
-
你能更好地格式化你的代码吗?
标签: node.js asynchronous