我正在使用 jQuery,并且知道这个问题是因为 jQuery.Deferred 实现不符合 Promises/A+ 标准。我不想使用任何其他库来解决这个问题。
有了这个,有没有办法从$.Deferred().fail()
回调使我返回到成功链?这可以通过以下的多重回调形式实现:then()
但到目前为止我还没有找到使用的解决方案.fail()
then:
asyncThatWillFail().then(function () {
// useless callback
}, function () {
console.log("error");
return $.Deferred().resolve();
}).then(function () {
console.log("continuing on success chain");
});
失败(不起作用):
asyncThatWillFail().fail(function () {
console.log("error");
return $.Deferred().resolve();
}).then(function () {
console.log("continuing on success chain");
});
就我而言,我只需要检查是否失败,设置一个标志,然后继续我正在做的事情。我根本不需要“then”示例中的并行成功处理程序。
这里有一个jsFiddle https://jsfiddle.net/86d29j1L/2/进一步澄清我的意思。
不,你不能使用.fail
为了那个原因。但是,您不需要将函数作为第一个参数传递给.then http://api.jquery.com/deferred.then/:
参数可以是null
如果不需要该类型的回调。
由于仅then
启用链接,您应该使用
asyncThatWillFail().then(null, function () {
console.log("error");
return $.Deferred().resolve();
}).then(function () {
console.log("continuing on success chain");
});
除了需要返回已履行的 jQuery 承诺之外,这就像ES6 then https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then方法其中catch https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch是同义词.then(null, …)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)