gpt4 book ai didi

JQuery函数返回promise但 'when'仍然不等待

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

我刚刚了解了 $.when().then() 功能。我一直在努力让它发挥作用,并且看了很多例子,但我显然还没有完全理解,因为我无法让它发挥作用。我知道无论我作为参数传递给 when() 函数的什么函数都必须返回一个 Promise 对象。我觉得我所做的应该有效,但效果不太明显,我仍然不明白一些事情。在下面的代码中,loadUserInterfaceGroupsDropDown()initializeUserInterfaceGroups() 内的 ajax 调用完成之前执行。请注意,我返回了 ajax 调用的结果。这应该产生与我看到的许多简单示例相同的结果,这些示例将 ajax 调用作为参数传递给 when() 语句。我还在底部的 else 语句中返回了一个 promise 。但由于这似乎不起作用,我显然误解了一些东西。请帮忙。

$.when(initializeUserInterfaceGroups()).then(loadUserInterfaceGroupsDropDown());

function initializeUserInterfaceGroups() {
if (userInterfaceGroups === undefined) {
// get the UserInterfaceGroups
return $.ajax({
type: "POST",
url: "Users.aspx/GetUserInterfaceGroups",
data: "{organizationId: " + $("#ddOrganization").val() + "}",
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
if (response.d.Success) {
userInterfaceGroups = response.d.UserInterfaceGroups;
//loadUserInterfaceGroupsDropDown();
renderCheckboxLists();
} else {
alert(response.d.ErrorMessage);
}
},
failure: function(response) {
alert('Error getting the UserInterfaceGroups: ' + response.d);
}
});
} else {
return $.Deferred().promise();
}
}

最佳答案

.when().then() Promise 与多个 ajax 函数一起使用的正确语法如下:

$.when($.ajax(...), $.ajax(...)).then(function (resp1, resp2) {
//this callback will be fired once all ajax calls have finished.
});

所以基本上你需要像上面那样改变你的调用方法。希望对您有帮助!

关于JQuery函数返回promise但 'when'仍然不等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44031832/

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