我有2个函数,应该一个接一个地调用,如下所示。
MainFunc().then(DrawChart());
MainFunc()函数内部有我在下面提到过的嵌套函数。
我希望MainFuc()返回promise,否则DrawChart()函数应该在createMultiBatchDropdown()完成后调用。
我检查了一些链接:Nesting asynchronous jquery promises,但我不想使用任何设置超时或延迟功能。
我对这个then()和promise() function.Any help的概念还不熟悉,我会很感激的。
function MainFunc(){
var r = $.Deferred();
var xhr = BatchTypeFilterList(data,id).then(function(res){
//Logic goes here
var impactXhr = GetImpactData(id).then(function(result){
var DropXhr = createMultiBatchDropdown('ImpactBatchSearch',result)
})
})
return r.promise(xhr);
}
function BatchTypeFilterList(){
var deferred = $.Deferred();
var xhr = $.ajax({
//Ajax Call
success:function(result){
deferred.resolve(result);
}
})
return deferred.promise(xhr);
}
function GetImpactData(){
var deferred = $.Deferred();
var xhr = $.ajax({
//Ajax Call
success:function(result){
deferred.resolve(result);
}
})
return deferred.promise(xhr);
}
function createMultiBatchDropdown(){
var batchDropDownDeferred = $.Deferred();
//No ajax call normal jquery logic to form dropdown
batchDropDownDeferred.resolve(data);
return batchDropDownDeferred.promise(xhr);
}
发布于 2018-08-30 06:47:44
GetImpactData
返回一个Promise
,但它没有与外部xhr
链接,这意味着调用MainFunc
将导致在调用createMultiBatchDropdown
之前解析的Promise
。相反,return
由createMultiBatchDropdown
创建的Promise
,以便它与整个Promise链正确地链接在一起。您还需要返回impactXhr
来完成链。更改为:
var xhr = BatchTypeFilterList(data, id).then(function(res) {
//Logic goes here
var impactXhr = GetImpactData(id).then(function(result) {
return createMultiBatchDropdown('ImpactBatchSearch', result);
})
return impactXhr;
})
https://stackoverflow.com/questions/52090689
复制相似问题