【问题标题】:How to call asynchronous function each time for each object in array param如何每次为数组参数中的每个对象调用异步函数
【发布时间】: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


【解决方案1】:

调用一次是因为你只调用了一次,传入整个数组:

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

如果你想获取元素数组中每个对象的配置价格,那么你必须为每个对象调用它

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

我还鼓励您使用 Node 样式的回调 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 */ })
})

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 2011-11-21
    • 2021-05-29
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多