gpt4 book ai didi

javascript - Dojo:延迟类型范围 'Hitch' 的正确方法

转载 作者:行者123 更新时间:2023-12-02 14:06:35 24 4
gpt4 key购买 nike

我在通过以下 Dojo 小部件的异步结构传递范围时遇到困难:

function callDef(){
//This function has the scope I need
var deferred = new Deferred();
//try to hitch 'this' to function2
DojoBaseLang.hitch(this,deferred.resolve(function2(1)));
deferred.then(DojoBaseLang.hitch(this, function(callback) {
callback.then(
function (desiredResult) {
//How to hitch callDef initial scope to function3?
function3(desiredResult);
},
function (err) {
// Do something when the process errors out
console.log(err);
})
}),
function (err) {
// Do something when the process errors out
console.log(err);
}
);
function function2(variable){
//callDef scope not passed by hitch :(
var dataStucture;
//deferredFunction is a function which returns type Deferred
return deferredFunction(hierarchyTableQuery, function(dataSet){
//some iterative maniupulations will be performed on dataStructure here
dataStructure = dataSet;

}).then(function (){
return dataStructure;
});
}

function function3(variable){
//need a way to also have scope in this method
//doing other stuff
}

如您所见,callDef 首先调用 function2,返回延迟,完成执行,然后将 function2 的 dataStructure 对象的结果传递给 function3。就延迟/异步行为而言,这一切都工作得很好,问题是被调用的 dojo/_base/lang.hitch 函数没有像通常那样将范围从一个函数传递到另一个函数,在此案例从 callDeffunction2。我还想将相同的范围传递到 function3 中。我的 require 语句是正确的,并且我有其他非异步 .hitch 调用,这些调用在同一小部件​​/文件中成功。

感谢您的帮助

最佳答案

您可能正在寻找


deferred.resolve(DojoBaseLang.hitch(this,function2));
deferred.then(DojoBaseLang.hitch(this, function(callback) {
callback(1).then(
DojoBaseLang.hitch(this,function3),
function (err) {
… // rest of the code

关于javascript - Dojo:延迟类型范围 'Hitch' 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40006223/

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