javascript - jQuery Promise - 即使未找到某些 json 也已完成启动

标签 javascript jquery promise

我正在使用 Promise,但有时在一种情况下我无法从各种原因获取 JSON,即使缺少一些 JSON,我怎样才能启动完成,目前使用此代码我只有失败的消息

$.when(
    arrayResults[0] ? $.getJSON("url") : null,
    arrayResults[1] ? $.getJSON("url") : null,
    arrayResults[2] ? $.getJSON("url") : null

).done(function () { }).fail(function () {
    console.log('Failed');
});

最佳答案

您可以使用deferred.always(cb):

$.when(
    arrayResults[0] ? $.getJSON("url") : null,
    arrayResults[1] ? $.getJSON("url") : null,
    arrayResults[2] ? $.getJSON("url") : null
)
.done(function () { console.log('I will run when the promise was resolved') })
.fail(function () { console.log('I will run when the promise was rejected') })
.always(function() { console.log('I will always fire, regardless of previous results') })

请在此处查看更多信息:https://api.jquery.com/deferred.always/

关于javascript - jQuery Promise - 即使未找到某些 json 也已完成启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48325281/

相关文章:

javascript - MVC4 Devexpress 排序、分组和过滤不起作用

javascript - 在 jQuery 中选择*特定*元素

javascript - 我如何从 jquery 中的多个 div 中选择特定的 div

javascript - 从中间件中的 promise 调用 'next()' 会导致 'next shouldn' 被多次调用'

javascript - 如何 promise 一个不包含回调参数的函数?

javascript - 预期等于结果时出现不可变的 Chai 断言错误

Javascript JSON 解析 unescape 引号?

javascript - window.location.href 更改后浏览器后退按钮无法正常工作

jquery - 如何使用 jQuery key up 和 google chrome 数字输入?

mysql - Promise reject 未捕获 Node mysql 错误