gpt4 book ai didi

jquery-deferred - jQuery - 延迟等待一组 ajax 请求以完成甚至失败

转载 作者:行者123 更新时间:2023-12-01 06:39:37 26 4
gpt4 key购买 nike

在许多ajax请求都完成后,无论它们是成功还是出错,如何执行函数?

我一直在尝试使用 $.when.apply(this, array)传递一组延迟的 jqXHR 对象。然而就像文档说的

In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately >fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be >unresolved at that point.



如何利用 jQuery 延迟对象始终等待所有 ajax 调用完成?

也许我应该创建自己的延迟来包装所有其他延迟?如果是这样,我不太清楚如何设置。

最佳答案

本着 Promise 规范在 future 可能会如何发展的精神,PromiseInspection对象,这是一个 jQuery 附加函数,它会告诉您所有 promise 何时完成,无论是履行还是拒绝:

(function() {    
// pass either multiple promises as separate arguments or an array of promises
$.settle = function(p1) {
var args;
if (Array.isArray(p1)) {
args = p1;
} else {
args = Array.prototype.slice.call(arguments);
}

return $.when.apply($, args.map(function(p) {
// make sure p is a promise (it could be just a value)
p = wrapInPromise(p);
// Make sure that the returned promise here is always resolved with a PromiseInspection object, never rejected
return p.then(function(val) {
return new PromiseInspection(true, val);
}, function(reason) {
// Convert rejected promise into resolved promise by returning a resolved promised
// One could just return the promiseInspection object directly if jQuery was
// Promise spec compliant, but jQuery 1.x and 2.x are not so we have to take this extra step
return wrapInPromise(new PromiseInspection(false, reason));
});
})).then(function() {
// return an array of results which is just more convenient to work with
// than the separate arguments that $.when() would normally return
return Array.prototype.slice.call(arguments);
});
}

// utility functions and objects
function isPromise(p) {
return p && (typeof p === "object" || typeof p === "function") && typeof p.then === "function";
}

function wrapInPromise(p) {
if (!isPromise(p)) {
p = $.Deferred().resolve(p);
}
return p;
}

function PromiseInspection(fulfilled, val) {
return {
isFulfilled: function() {
return fulfilled;
}, isRejected: function() {
return !fulfilled;
}, isPending: function() {
// PromiseInspection objects created here are never pending
return false;
}, value: function() {
if (!fulfilled) {
throw new Error("Can't call .value() on a promise that is not fulfilled");
}
return val;
}, reason: function() {
if (fulfilled) {
throw new Error("Can't call .reason() on a promise that is fulfilled");
}
return val;
}
};
}
})();

然后,您可以像这样使用它:
$.settle(promiseArray).then(function(inspectionArray) {
inspectionArray.forEach(function(pi) {
if (pi.isFulfilled()) {
// pi.value() is the value of the fulfilled promise
} else {
// pi.reason() is the reason for the rejection
}
});
});

请记住, $.settle()将始终满足(从不拒绝)并且满足的值是 PromiseInspection 的数组对象,您可以询问每个对象以查看它是被满足还是被拒绝,然后获取相应的值或原因。有关示例用法,请参见下面的演示:

工作演示: https://jsfiddle.net/jfriend00/y0gjs31r/

关于jquery-deferred - jQuery - 延迟等待一组 ajax 请求以完成甚至失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152595/

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