$q 解析函数不触发

$q resolve function not triggering then

我以为我正确使用了 AngularJS $q 界面。但是我传递给 then 回调的函数永远不会被执行。我在 $q 包装的函数中调用 resolve。我已经验证 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();