for循环中的异步回调 - 节点js

时间:2013-07-31 18:10:00

标签: javascript node.js asynchronous for-loop callback

节点js for循环中的异步回调会产生问题:

for(var index in workload.elements) {
    var arr = [];
    var resourceIdentifiers = {};
    var elementinfo = {};
    var metadataModified = {};

    elementinfo = workload.elements[index];

    arr[index] = workload.elements[index].uri;

    if (workload.elements[index].parameters.imageUri) {
        arr.push(workload.elements[index].parameters.imageUri);
    }
    resourceIdentifiers = arr.join(',');

    console.log('uri' + resourceIdentifiers);

    mysql.elementlevelpricing(resourceIdentifiers, function(result){

        elementlevelpricingSummary = result;

        metadataModified = workload.elements[index].metadata;

        metadataModified.newitem = elementlevelpricingSummary;

        delete elementinfo.metadata;

        elementinfo.metadata = metadataModified;

        console.log('resultout' + JSON.stringify(elementinfo,null,2));

    });
}

在上面的代码中,workloads.elements是包含多个值的数组。但是我只得到其中一个值的回调结果。

0 个答案:

没有答案