gpt4 book ai didi

javascript - $q 解析函数此时未触发

转载 作者:行者123 更新时间:2023-12-03 10:44:07 24 4
gpt4 key购买 nike

我以为我正在使用 AngularJS $q接口(interface)正确。但我传递给 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();

关于javascript - $q 解析函数此时未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com