如何确保在所有3个异步请求完成后执行self.loadable()。 ???
var imghtml = "<span class='pull-right' style='padding-right:25px'><img alt='track' src='app/images/Icons/Track.png'><img alt='expand' src='app/images/Icons/Expand.png'></span>";
var request1 = {};
if(self.serviceid != null)
request1.healthIssue = {id:self.serviceid};
request1.location = {id:self.locationid};
request1.time = {id:header.defaultduration().value};
request1.hospital = {id:header.defaulthospital().value};
request1.query = {groupName:'speciality', dimension:'VISITS', viewBy:'MARKETSHARE'};
console.log(request1);
server.fetchData(request1).done(function(data){
console.log('The specialty marketshares are : ');
console.log(data);
if(data.length != 0) {
$.each(data, function(index,mshare){
var count = 0;
$.each(self.marketsharetable(), function(index, tobj) {
if(tobj.specialty == mshare.name){
tobj.marketshare(mshare.value);
count++;
return false;
}
});
if(count == 0){
var obj = {};
obj.specialty = mshare.name;
obj.marketshare = ko.observable(mshare.value);
obj.rank = ko.observable();
obj.volume = ko.observable();
obj.btns = imghtml;
self.marketsharetable.push(obj);
}
});
console.log(self.marketsharetable());
}
else{
console.log("Clearing the table");
$('#msharetable').DataTable({
"paging": false,
"info": false,
"destroy": true,
"data": self.marketsharetable(),
"language": {
"emptyTable": "No data available"
}
});
}
});
var request2 = {};
if(self.serviceid != null)
request2.healthIssue = {id:self.serviceid};
request2.location = {id:self.locationid};
request2.time = {id:header.defaultduration().value};
request2.hospital = {id:header.defaulthospital().value};
request2.query = {groupName:'speciality', dimension:'VISITS', viewBy:'RANK'};
server.fetchData(request2).done(function(data){
console.log('The specialty ranks are : ');
console.log(data);
$.each(data, function(index,mrank){
var count = 0;
$.each(self.marketsharetable(), function(index, tobj) {
if(tobj.specialty == mrank.name){
tobj.rank(mrank.value);
count++;
return false;
}
});
if(count == 0){
var obj = {};
obj.specialty = mrank.name;
obj.marketshare = ko.observable();
obj.rank = ko.observable(mrank.value);
obj.volume = ko.observable();
obj.btns = imghtml;
self.marketsharetable.push(obj);
}
});
console.log(self.marketsharetable());
});
var request3 = {};
if(self.serviceid != null)
request3.healthIssue = {id:self.serviceid};
request3.location = {id:self.locationid};
request3.time = {id:header.defaultduration().value};
request3.hospital = {id:header.defaulthospital().value};
request3.query = {groupName:'speciality', dimension:'VISITS', viewBy:'COUNT'};
server.fetchData(request3).done(function(data){
console.log('The specialty input volumes are : ');
console.log(data);
$.each(data, function(index,mvolume){
var count = 0;
$.each(self.marketsharetable(), function(index, tobj) {
if(tobj.specialty == mvolume.name){
tobj.volume(mvolume.value);
count++;
return false;
}
});
if(count == 0){
var obj = {};
obj.specialty = mvolume.name;
obj.marketshare = ko.observable();
obj.rank = ko.observable();
obj.volume = ko.observable(mvolume.value);
obj.btns = imghtml;
self.marketsharetable.push(obj);
}
});
console.log(self.marketsharetable());
$('#msharetable').DataTable({
"paging": false,
"info": false,
"destroy": true,
"data": self.marketsharetable(),
"language": {
"emptyTable": "No data available"
},
"deferRender": true,
"columns": [
{ "data": "specialty" },
{ "data": "marketshare" },
{ "data": "rank" },
{ "data": "volume" },
{ "data": "btns" }
]
});
});
self.loadtable();
请仅在对服务器进行3次异步调用后才能执行同步功能。我是高级javascript的新手。建议请?
答案 0 :(得分:1)
在JavaScript中调用scynchronous函数时,它通常会返回一个promise。正如我在您的代码中看到的那样,server.fetchData(request)
调用会返回承诺,因为您使用的是承诺中的典型.done
。
您可以将promise存储在变量中,如下所示:
var promise1 = server.fetchData(request);
然后像这样使用它:
promise1.done(function() { /* your code here */ });
完成承诺后,内部函数将运行。履行(或拒绝)承诺的责任在异步方法中。即当请求完成并且响应已到达时,异步方法将履行承诺。并且,在他的情况下,将通过提供对回调的响应来实现它,即
promise.done(function(response) { /* use response here */});
您可以了解更多有关查看jQuery docs on deferred(这是实现承诺的jQuery方式)或google搜索“javascript promises”的承诺。延迟具有promise
的所有功能,以及履行(resolve
)或拒绝(reject
)承诺的方法,可以通过调用.promise()
返回推迟的方法。
例如,asynchronouse方法可能如下所示:
function asynch() {
var deferred = $.Deferred();
// do something asynchronous, and get some data
// if the result is ok, fulfill the promise
deferred.resolve(data);
// if it failed, reject it
deferred.reject(reason);
return deferred.promise();
};
这个想法是,如果你调用这个方法,它将立即返回一个promise。并且,当异步代码(如AJAX调用)完成时,返回它的方法将承诺reject
或resolve
。发生这种情况时,将调用附加到承诺的done
或fail
回调。
现在您已经了解了这些基础知识,jQuery提供了一种组合承诺的方法,即jQuery.when()。您可以编写如下代码:
var promise1 = server.fetchData(request1);
var promise2 = server.fetchData(request2);
var promise3 = server.fetchData(request3);
$.when(promise1, promise2, promise3)
.done(function(result1, result2, result3) {
// use the results of the three server.fetchData here
})
完成部分只有在满足三个承诺时才会运行。如果它们中的任何一个失败,它将无法运行。
您应该始终处理您的承诺的.fail
。 server.fetchData
可能由于多种原因而失败,您的代码应该处理这些问题。
还有另一个有趣的承诺库,like Q,现代浏览器本身支持它们:native promises。它们都共享Promises/A+中定义的基本概念。