我正在使用 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/