gpt4 book ai didi

javascript - 跨不同的 obj 属性设置 deferred/promise

转载 作者:行者123 更新时间:2023-12-02 17:27:18 27 4
gpt4 key购买 nike

使用 jquery 1.6.*

我正在使用模块式模式。我希望能够在“UNI.obj1”中创建和解析延迟并能够使用它们,以便我可以在“UNI.obj2”对象中启动行为。

假设我有以下对象:

var UNI = {};

UNI.obj1 = function() {
// set up a deferred here;

var method1 = function(){
// RESOLVE deferred here

}
var method1A = function(){
window.onload = function(){
method1();
}
}()
}()

UNI.obj2 = function(o) {
var method2 = function(){
// Use the PROMISE of the resolve here

}
}(UNI.obj1);

最佳答案

定义您的延迟对象并返回它的 promise ,然后对其采取行动。

var UNI = {};

UNI.obj1 = function () {
// set up a deferred here;
var def = $.Deferred();

var method1 = function () {
// RESOLVE deferred here
def.resolve();

return def.promise();
}
var method1A = function () {
window.onload = function () {
method1();
}
}()
return def.promise();
}

UNI.obj2 = function (o) {
var method2 = function () {
// Use the PROMISE of the resolve here
o().done(function () {
// it was resolved!
console.log("Resolved!");
});
}()
}(UNI.obj1);

关于javascript - 跨不同的 obj 属性设置 deferred/promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23417689/

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