gpt4 book ai didi

javascript - done() 也适用于整数吗?

转载 作者:行者123 更新时间:2023-11-30 08:42:09 24 4
gpt4 key购买 nike

我在 javascript (jquery) 中有这段代码

function getTopLevelId(gender) {
var catId = $.ajax({
type: 'GET',
url: '{url}'
dataType: 'html'
});

return catId; //Returns defered object
}

function getSelectedCategoryId() {
return 7; //returns int based on value in an element
};


function mainFuncion(id) {
if (parseInt(id) === 0) {
catObj= getTopLevelId(gender); //Returns defered object
}
else {
catId = getSelectedCategoryId(id); //Returns int
catObj = catId;
}

$.when(catObj).done(function(catId) {
//Doing some stuff and want to use catId as a category id
}
}

我的问题是关于 done() 中的“做一些事情”。它对我来说SEEMS 就像 done() 被调用 并给我正确的类别 ID,即使 catId 只是一个 int,但这是否总是正确的?或者有没有其他方法可以“更安全”地实现我在 mainFunction 中想要的东西?

最佳答案

是的。如果没有参数传递给 $.when()是未解析的 promise 对象,然后立即调用 done 处理程序。

If a single argument is passed to jQuery.when and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. The doneCallbacks are passed the original argument. In this case any failCallbacks you might set are never called since the Deferred is never rejected.

var a = 7;
$.when(a).done(function (x) {
console.log('inside', x)
});
console.log('after')

演示:Fiddle , Fiddle2

关于javascript - done() 也适用于整数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25379301/

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