在下面的函数中,我试图返回第二次异步调用的promise对象,但是typescript报告函数必须返回值。我不确定我如何才能做到这一点?我如何退还第二个承诺?
private getSalesUrl<TServiceInput>(serviceCall: (args: TServiceInput) => JQueryPromise<IServiceResponseT>, args): JQueryPromise<any> {
serviceCall(args).done(result => { //1st async call
if (result.serviceOutput) {
if (result.serviceOutput.key === "URL") {
return tipsInterop.executeSalesRequest(result.serviceOutput.value); //2nd async call
}
}
});
}发布于 2017-07-18 03:18:13
var requests = {
firstRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/1');
},
secondRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/2');
}
};
var processResults = {
callService: function() {
return requests.firstRequest().then((data, textStatus, promise) => {
return requests.secondRequest().then((data2, textStatus2, promise2) => {
return "This is the content of second call: id=>" + data2.id;
});
});
}
};
processResults.callService().then((data, textStatus, promise) => {
console.log(data);
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
注意:
return "This is the content of second call: id=>"+ data2.id;返回数据,则会将其包装到@Bergi评论的新promise中)。
return promise2代替intercept下面是您将其与return promise2;互换时的显示方式
var requests = {
firstRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/1');
},
secondRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/2');
}
};
var processResults = {
callService: function() {
return requests.firstRequest().then((data, textStatus, promise) => {
return requests.secondRequest().then((data2, textStatus2, promise2) => {
return promise2;
});
});
}
};
processResults.callService().then((data, textStatus, promise) => {
console.log(data);
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
https://stackoverflow.com/questions/45136468
复制相似问题