angular.forEach(tableName, function(value, key){
console.log("value ", value)
getFormsService.getTableData(value).then(function(dynResponse){
console.log("key",key," dynResponse", dynResponse.data)
});
});
在上面的代码中, tableName 包含表名数组 例如:tableName = [" table1"," table2"," table3"," tale4"]
每张表有50,10,5,20条记录
我的输出:
"value ", table1
"value ", table2
"value ", table3
"value ", table4
"key",2," dynResponse", array(5 records)
"key",1," dynResponse", array(10 records)
"key",3," dynResponse", array(20 records)
"key",0," dynResponse", array(50 records)
预期产出:
"value ", table1
"key",0," dynResponse", array(50 records)
"value ", table2
"key",1," dynResponse", array(10 records)
"value ", table3
"key",2," dynResponse", array(5 records)
"value ", table4
"key",3," dynResponse", array(20 records)
help me to get expected output
Thanks
答案 0 :(得分:0)
preserveAspectRatio
我不确定,你想要做什么。要同步执行服务请求,您可以使用vw
angular.forEach(tableName, function(value, key){
getFormsService.getTableData(value).then(function(dynResponse){
console.log("value ", value)
console.log("key",key," dynResponse", dynResponse.data)
});
});
服务方法。
答案 1 :(得分:-1)
这里getFormsService.getTableData
回调是异步调用。因此,循环线程和服务线程不会链接在一起而是将其另一个线程连接在一起。
// this line does not wait for service callback to be completed.
angular.forEach(tableName, function(value, key){
// this is an a asynchronous call
getFormsService.getTableData(value).then(function(dynResponse){
console.log("key",key," dynResponse", dynResponse.data)
});
});
您可以更改服务以进行一次通话或使用此类内容
var inUse=false;
angular.forEach(tableName, function(value, key){
while (!inUse) {
//waiting call might be costly you can try wait here as well
}
inUse=true;
// this is an a asynchronous call
getFormsService.getTableData(value).then(function(dynResponse){
console.log("key",key," dynResponse", dynResponse.data)
inUse=false;
});
});
答案 2 :(得分:-1)
JavaScript始终是同步和单线程的。由于您正在调用服务代码,因此不会等到获得响应。它会执行其余的代码,当收到响应时,它会执行剩下的代码。
JavaScript只是异步,因为它可以进行Ajax调用。代码将停止执行,直到调用返回(成功或否则),此时回调将同步运行。
function FirstFunction(callback) {
angular.forEach(tableName, function(value, key){
console.log("value ", value);
});
callback();
}
FirstFunction(function() {
getFormsService.getTableData(value).then(function(dynResponse){
console.log("key",key," dynResponse", dynResponse.data)
});
});