【问题标题】:$q resolve function not triggering then$q 解析函数没有触发然后
【发布时间】:2015-02-20 15:14:27
【问题描述】:

我以为我正确使用了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 单元测试。

【问题讨论】:

  • 你能添加一个拒绝,看看是否触发,为什么?
  • @Dayan Moreno Leon:这样做并更新了问题。 catch 功能也没有发生。
  • 是的,错过了这是测试的部分,Remco 是对的,您需要使用 $apply 或 $digest 触发 angulars 摘要循环

标签: javascript unit-testing angular-promise


【解决方案1】:

您需要致电$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();

【讨论】:

    猜你喜欢
    • 2020-05-23
    • 2021-09-25
    • 2014-04-24
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多