[英]Q.all promise never runs then callback
I am bit lost as to how Q.all
works. 我对Q.all
工作方式Q.all
。 I have the following code snippet ( jsfiddle ): 我有以下代码片段( 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");
});
The first call to callUrl
will succeed, the second will fail (will return an error as the url does not exist). 首次调用callUrl
将成功,第二次将失败(由于URL不存在,将返回错误)。 I am expecting the Q.all
to execute the then
callback like what ajax.always
does but this isn't the case. 我期望Q.all
像ajax.always
一样执行then
回调,但事实并非如此。
Am I doing something wrong? 难道我做错了什么?
You need to chain a catch
method and callback for that, because Q.all
will return a rejected promise when it encounters a rejected promise. 您需要为此链接catch
方法和回调,因为Q.all
在遇到被拒绝的承诺时将返回被拒绝的承诺。
You get called back concerning a rejected promise via catch
(or the optional second callback you can pass to then
). 您会因catch
(或您可以传递给then
的可选第二个回调)而回信有关被拒绝的诺言。
The 404 is an error that has propogated from the server, into your JS application. 404错误已从服务器传播到您的JS应用程序中。
use .catch
使用.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.