在使用http.get()迭代调用url并使用$q.all()解析时
In calling URLs iteratively using http.get() and resolving using $q.all()
我正在实现这个场景,我必须迭代地从多个url获取数据,并使用一些业务逻辑处理它并在屏幕上显示。我在控制器中实现这一点,因为它是一个要求。一切都很好,直到第1部分,我得到了promises
数组中的6个承诺对象。但是,我没有把数据放入metricData
。在浏览器中运行时,我在控制台中看到null
。我确信数据来自URL响应。我觉得我在$q.all
方法中做了一些愚蠢的事情。这是正确的吗?
var calculateMutationsInDepth = function(){
//Part-1
var promises=[];
var metricData=[];
for(var depth=0 ; depth<6 ; depth++){
var resourceUrl = urlService(depth);
promises.push($http.get(resourceUrl)
.then(function(response){
return response.data;
},function(status){
return status;
}));
}
//Part-2 Resolving the promise array below
$q.all(promises).then(function(data){
for(var eachResult=0; eachResult < data.length; eachResult++){
if(null != data[eachResult]){
var eachDataObject = data[eachResult];
//For debugging console.log(eachDataObject);
for(var objCount=0; objCount < eachDataObject.length; objCount++){
if(eachDataObject[objCount].scope === "PRJ" || eachDataObject[objCount].scope === "FIL")
metricData.push(eachDataObject[objCount]);
}
}
}
});
if(metricData != null){
analyzeMutationData(metricData); //Calling a function with the aggregated data array where business logic is present
}
};
calculateMutationsInDepth(); //Calling the above function
是的,一些愚蠢的事情。
如前所述,analyzeMutationData(metricData)
是同步调用的,而metricData
是在$q.all(promises).then()
回调中异步填充的。
同样,错误处理程序function(status){ return status; }
也不合适。:
- 完全省略错误处理程序,并允许任何单个
$http
错误阻止第2部分中的进一步处理,或者 -
return null
,允许在第2部分中进行处理,并在第2部分中进行if(dataObject != null)
测试,以过滤掉任何此类错误。
下面是修改后的代码,加上一些其他的修饰,并演示了如果calculateMutationsInDepth()
返回一个promise可以做什么。
var calculateMutationsInDepth = function() {
//Part-1
var depth, promises = [];
for(depth=0; depth<6; depth++) {
promises.push($http.get(urlService(depth))
.then(function(response) {
return response.data;
}, function(error) {
return null; // error recovery - `dataObject` below will be null
}));
}
//Part-2 Aggregate the promises, extract metric data and apply business logic
return $q.all(promises).then(function(data) { // note `return` here
var dataObject, i, j, metricData = [];
for(i=0; i<data.length; i++) {
dataObject = data[i];
if(dataObject != null) {
for(j=0; j<dataObject.length; j++) {
if(dataObject[j].scope === "PRJ" || dataObject[j].scope === "FIL") {
metricData.push(dataObject[j]);
}
}
}
}
// Analyse here, inside the .then()
if(metricData.length > 0) { // metricData is an array and will never be null, therefore test metricData.length.
return analyzeMutationData(metricData);
}
return null;
});
};
calculateMutationsInDepth().then(function(analysis) {
// all complete
// `analysis` is either null or whatever `analyzeMutationData(metricData)` returned.
}).catch(function(error) {
console.log(error);
});
希望这对你有所帮助!如果没有,请告诉我
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- Node.js中的Promise.all没有't调用then函数
- Node.js Express - app.all(“*”, func) 在访问根域时不会被调用
- 为什么在 Promise.all() 之后没有调用 onJect,其中数组中包含的 Promise.reject() 传
- Angular $q.all 在第一个承诺完成后被调用
- nodejs Q.all承诺函数调用本身
- 如何解决错误'ext-all.js Uncaught TypeError:无法调用方法'getProxy
- 捕获多个.all()调用中的错误
- 在AngularJS中为all $https调用设置相对路径
- 在使用http.get()迭代调用url并使用$q.all()解析时
- 如何在fabric.all.min.js中调用弯曲文本中的字体族