jQuery完成延迟后从函数返回 [英] jquery return from function after completing deferred
本文介绍了jQuery完成延迟后从函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下函数,并且我想确保在所有这些行均已执行后该函数返回:
I am working on the the following function, and I want to make sure that the function returns after all these lines have been executed:
var cinfo = iClassInfo.instance().iClassById(nObject.dclass, true);
现在函数将返回而不执行此行,因此nObject.mClassName始终是未定义的.提前致谢!!
Right now the function returns without executing this line, and hence nObject.mClassName is always undefined. Thanks in advance!!
getObj : function(options) {
var self = this,
$master = $.Deferred(),
$deferreds = [];
return self._wt.getIterativeResults({
storage: self._str,
resSetName: self._resultSetName,
startIndex: options.startIndex,
endIndex: options.endIndex,
idForm: 1
}).then(
function(objects){
var nObjects = [];
$.each(objects, function(i,object){
var nObject = object.nObject;
if(nObject.dclass != null) {
var $deferred = iClassInfo.instance().$intializedDef;
$deferreds.push($deferred);
$deferred.done(function(deferreddone){
var cinfo = iClassInfo.instance().iClassById(nObject.dclass, true);
if(cinfo != null) {
nObject.mClassName = cinfo.name;
}
});
}
nObject.pp = object.npp;
nObjects.push(nObject);
});
$.when($deferreds).done(function(){
$master.resolve(nObjects);
});
return $master.promise();
},
function(resp) {
}
);
},
推荐答案
将$deferreds
数组与$.when()
一起使用时存在问题,请尝试
There is a problem in how you were using the $deferreds
array with $.when()
so try
getObj: function (options) {
var self = this,
$master = $.Deferred(),
$deferreds = [];
return self._wt.getIterativeResults({
storage: self._str,
resSetName: self._resultSetName,
startIndex: options.startIndex,
endIndex: options.endIndex,
idForm: 1
}).then(function (objects) {
var nObjects = [];
$.each(objects, function (i, object) {
var nObject = object.nObject;
if (nObject.dclass != null) {
var $deferred = iClassInfo.instance().$intializedDef;
$deferreds.push($deferred);
$deferred.done(function (deferreddone) {
var cinfo = iClassInfo.instance().iClassById(nObject.dclass, true);
if (cinfo != null) {
nObject.mClassName = cinfo.name;
}
});
}
nObject.pp = object.npp;
nObjects.push(nObject);
});
//there was a problem here
$.when.apply($, $deferreds).done(function () {
$master.resolve(nObjects);
});
}, function (resp) {
$master.reject();
});
return $master.promise();
}
这篇关于jQuery完成延迟后从函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文