节点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是包含多个值的数组。但是我只得到其中一个值的回调结果。