繁体   English   中英

Q.all promise永远不会运行然后回调

[英]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.allajax.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM