[英]How to wait for the last keystroke before executing a function in Angular?
[英]Angular - Wait function before continue executing?
我有一个类似于以下的角度函数。 有一个循环,每次我执行POST
请求并将respose合并到变量中:
async loop(jsonData){
var text: any;
for(var i=0; i<jsonData.Hoja1.length; i++){
var id = jsonData.Hoja1[i].id.toString();
var codigo = Number(jsonData.Hoja1[i].codigo).toString();
var featureRequest = new WFS().writeGetFeature();
fetch('http://localhost:3000/inspire', {
method: 'POST',
body: new XMLSerializer().serializeToString(featureRequest)
}).then((response) => {
return response.json();
}).then((json) => {
var features = new GeoJSON().readFeatures(json);
if(features[0]){
var format = new WKT();
var wktRepresenation = format.writeGeometry(features[0].getGeometry());
var consulta = "UPDATE " + wktRepresenation + ";";
text = text + consulta;
}
});
}
return text;
}
我正在尝试打印函数结果,但是它返回了undefined
值。
this.loop(jsonData).then( res => {console.log(res)});
似乎.then
不会等到loop
函数结束执行,或者我在loop
函数内做错了什么(也许是POST
调用?)。
请指导我,我对同步非常了解。 如何获得正确的值? 谢谢。
在获取之前先写await
。 await fetch('http://localhost:3000/inspire', ...
因为您必须在每次循环迭代中等待完成的访存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.