[英]unit testing angularjs $q.all - promise never completes
[英]Q.all promise never runs then callback
我对Q.all
工作方式Q.all
。 我有以下代码片段( jsfiddle ):
function callUrl(remoteEndPoint){
return $.ajax({
url: remoteEndPoint,
method: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
console.dir(data);
},
error: function (xhr, textStatus, errorThrown) {
console.error("error");
}
});
}
Q.all([callUrl("https://httpbin.org/get"), callUrl("https://httpbin.org/undefined")]).then(function(){
console.log("Done");
});
首次调用callUrl
将成功,第二次将失败(由于URL不存在,将返回错误)。 我期望Q.all
像ajax.always
一样执行then
回调,但事实并非如此。
难道我做错了什么?
您需要为此链接catch
方法和回调,因为Q.all
在遇到被拒绝的承诺时将返回被拒绝的承诺。
您会因catch
(或您可以传递给then
的可选第二个回调)而回信有关被拒绝的诺言。
404错误已从服务器传播到您的JS应用程序中。
使用.catch
Q.all([callUrl("https://httpbin.org/get"), callUrl("https://httpbin.org/undefined")]).then(function(){
console.log("Done");
}).catch(function(e){
console.log("err",e);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.