我以为我在正确使用AngularJS $q界面。但是我传递给then
回调的函数从未执行过。我在resolve
包装的函数中调用$q
。我已经验证了resolve()
被调用,但是似乎与.then()
链接的函数不同:
$q(function(resolve, reject) {
resolve();
reject();
console.log("This happens"); //This shows that resolve() is being called
}).then(function() {
console.log("This doesn't"); //Yet this never gets executed
}).catch(function() {
console.log("This also doesn't happen");
});
输出为:
This happens
我究竟做错了什么?
我的环境是使用mocha和sinon-chai进行的KarmaJS单元测试。
最佳答案
您需要致电$rootScope.$apply()
。
$q(function(resolve, reject) {
resolve();
console.log("This happens"); //This shows that resolve() is being called
}).then(function() {
console.log("This too!");
});
$rootScope.$apply();