gpt4 book ai didi

jquery - 相当于 jQuery.when 允许失败

转载 作者:行者123 更新时间:2023-12-01 05:26:28 25 4
gpt4 key购买 nike

我发现当我的多个请求全部成功时,它们会正常工作,但如果其中一个请求由于某种原因失败,我的回调将不会执行。我查了一下 jQuery 文档,上面写着

In the case where multiple Deferred objects are passed to jQuery.when(), the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected

这意味着,如果我提交 n 个请求并且它们都成功,则主对象将触发成功解析,以及连接的回调(使用 .then.done) 将触发。但是,如果这些 n 请求中的任何一个失败,主 Promise 将被拒绝,并且当其他子 Promise 可能仍在工作时,我将调用failCallback。此外,除非我使用闭包,否则我将无法访问回调中的这些子 promise 。

在 jQuery 中提交多个 ajax 请求并且无论其中任何一个失败都具有相同行为的正确方法是什么?

最佳答案

我一开始并没有建议这样做,因为您似乎正在使用可以单独列出的 promise /延迟,但使用 $.apply() 可能会起作用。这将“单独”运行数组中的所有项目。

var promiseArray = [ajax1, ajax2, ...];
$.when.apply($, promiseArray).then(function() { //do the final stuff }

即使其中一个有问题,也应该运行所有这些并运行 .then() 。

关于jquery - 相当于 jQuery.when 允许失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39472294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com