问题描述
我已经检查有关承诺的答案数,但我不能让我的code工作(也许是我在做什么可怕的错误在其他地方)
I have checked number of answers regarding promises, but I can't get my code working (perhaps I'm doing something terribly wrong in other place)
在一般我正在使用AngularJS和SignalR小的测试应用程序。我有signalR服务,它看起来是这样的:
In general I'm working on small test application using AngularJS and SignalR. I have signalR service which looks like this:
(function () {
'use strict';
var serviceId = 'signalRSvc';
angular.module('app').service(serviceId, ['$rootScope', signalrcontext]);
function signalrcontext( $rootScope) {
var performanceHub = null;
var connection = null;
var service = {
initialize: initialize,
getPerformanceCounters: getPerformanceCounters,
getAllValues: getAllValues
};
return service;
function initialize() {
connection = $.connection;
performanceHub = connection.webServicePerformanceHub;
connection.hub.logging = true;
performanceHub.client.updatePerformanceData = function(performanceData) {
$rootScope.$emit("updatePerformanceData", performanceData);
};
return connection.hub.start();
};
function getPerformanceCounters() {
return performanceHub.server.getPerformanceCounters();
};
function getAllValues(id) {
return performanceHub.server.getAllValues(id);
};
}
})();
我想要做的是,用来初始化SignalR,然后执行方法getPerformanceCounters(),这将下载计数器渲染(返回为对象数组)的列表,然后为每个计数器我想获得的数据,这里的问题开始。根据MS文档,SignalR代理方法返回的承诺。我已经写了这个code,但我不知道它为什么不一步getAllValues工作(按照这个答案的它应该工作)
function initSignalR() {
return signalRSvc.initialize().then(function() {
return signalRSvc.getPerformanceCounters();
}).then(function(configurations) {
log('performance counters configuration downloaded');
return getAllValues(configurations);
}).then(function (resultData) {
vm.resultData = resultData;
});
}
function getAllValues(configurations) {
var promises = new Array();
angular.forEach(configurations, function(configuration) {
promises.push(signalRSvc.getAllValues(configuration.Id));
});
return $q.all(promises);
}
在我的理解,然后最后当所有的呼吁,以signalRSvc.getAllValues完成并resultData应包含那些承诺返回的对象数组应该执行。取而代之的是,我得到一些垃圾这甚至不是一个数组。
In my understanding, last then should execute when all 'calls' to signalRSvc.getAllValues are completed and resultData should contain array of objects returned from those promises. Instead of this, I'm getting some garbage which is not even an array.
令人惊讶的(对我来说当然),当我做这样的事情。
Surprisingly (for me of course) when I do something like this
function getAllValues(configurations) {
var promises = new Array();
angular.forEach(configurations, function(configuration) {
promises.push(signalRSvc.getAllValues(configuration.Id));
});
return $q.all(promises).then(function(resultData) {
//here result data is fine!!
});
}
在嵌套那么结果数据是好的(当然解决的承诺的顺序乱了)。
in nested then result data is fine (but of course the order of resolving promises is messed up).
因为我感谢提前帮忙出出主意。
Thanks for help in advance since i'm out of ideas.
推荐答案
它看起来像(从我的团队我的同事发现这一点)的问题涉及由SingalR返回的承诺。 SignalR将返回jQuery的承诺,这是不符合AngularJS承诺所有情况下兼容。溶液包裹从signalR代理方法是 $ q.when 。现在,一切工作正常。
It looks like the problem was related to the promises returned by SingalR (my colleague from my team found this) . SignalR is returning jQuery promises which are not "compatible" in all cases with AngularJS promises. Solution was to wrap methods from signalR proxy with $q.when. Now everything is working fine.
固定code(其中仍包含一些其他的SignalR相关的问题)
Fixed code (which still contains some other SignalR related issues)
.....
return service;
function initialize() {
connection = $.connection;
performanceHub = connection.webServicePerformanceHub;
connection.hub.logging = true;
performanceHub.client.updatePerformanceData = function(performanceData) {
$rootScope.$emit("updatePerformanceData", performanceData);
};
return $q.when(connection.hub.start();
};
function getPerformanceCounters() {
return $q.when(performanceHub.server.getPerformanceCounters());
};
function getAllValues(id) {
return $q.when(performanceHub.server.getAllValues(id));
};
.....
无极链这是行不通的previously现在工作正常。
Promise chaining which was not working previously is now working fine
function initSignalR() {
return signalRSvc.initialize().then(function () {
return signalRSvc.getPerformanceCounters();
}).then(function (configurations) {
chartConfigurations = configurations;
return getAllValues(configurations);
}).then(function (chartData) {
angular.forEach(chartData, function(value, key) {
chartConfigurations[key].chartData = convertToChartDataset(value);
chartConfigurations[key].options = {
animation: false
};
});
vm.configurations = chartConfigurations;
});
}
function getAllValues(configurations) {
var promises = [];
$.each(configurations, function (index, value) {
promises.push(signalRSvc.getAllValues(value.Id));
})
return $q.all(promises);
}
这篇关于AngularJS承诺$ q.all和SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!