函数在返回之前是否会等待异步函数完成? [英] Will the function wait for the asynchronous functions completion before returning?

查看:91
本文介绍了函数在返回之前是否会等待异步函数完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function myFunction(marker1,marker2){
var firstAddress =空值;
var secondAddress = null;
geocoder.geocode({'latLng':marker1.getPosition()},function(results,status){
#如果geocoding成功,设置firstAddress
})

geocoder.geocode({'latLng':marker2.getPosition()},function(results,status){
#如果地理编码成功,set secondAddress
})
return [firstAddress, secondAddress];

}



code> myFunction的。地理编码调用是异步的,对吧?所以它会很快执行并返回,并在结果上调用回调函数。当我返回地址时,地理编码是否完成?我怎么才能确保这两个函数在返回之前完成? 解决方案

简短回答:不。在AJAX完成之前,您几乎可以立即到达 myFunction ,很可能 well 的返回行。



您最好的办法是强制geoCoder的请求保持同步。如果你不能,那么你需要做的是设置一个全局标志,指示两个请求何时完成并等待该标志被设置。如果是的话,那么你可以收集你的数据,构建你的数组,并返回它。

  function myFunction(marker1,marker2) {
var firstAddress = null;
var secondAddress = null;

document.geoCodeRequestCompleteFlag = 0;

geocoder.geocode({'latLng':marker1.getPosition()},function(results,status){
#如果geocoding成功,设置firstAddress
})

geocoder.geocode({'latLng':marker2.getPosition()},function(results,status){
#如果地理编码成功,设置secondAddress
})

setTimeout(function(){
document.geoCodeRequestCompleteFlag = -1;
},15000); // - 确保我们不会无限期卡住

while(document.geoCodeRequestCompleteFlag< 2&&document.geoCodeRequestCompleteFlag> 0){
// wait


if(document.geoCodeRequestCompleteFlag< 0){
return'timeout';
} else {
return [firstAddress,secondAddress];
}
}

一定要添加一个您的 marker1.getPosition() marker2.getPosition() callbacks中的document.geoCodeRequestCompleteFlag ++

这个缺点是,如果你的请求需要很长时间,浏览器可能会终止你的脚本。


Let's say I have these geocoding calls:

function myFunction(marker1,marker2) {
  var firstAddress  = null;
  var secondAddress = null;
  geocoder.geocode({'latLng': marker1.getPosition()}, function(results, status) {
    # if geocoding successful, set firstAddress
  })

  geocoder.geocode({'latLng': marker2.getPosition()}, function(results, status) {
    # if geocoding successful, set secondAddress
  })
  return [firstAddress,secondAddress];

}

Let's say I call myFunction. The geocode call is asynchronous, right? So it will execute and return quickly, calling the callback on results. Will the geocoding have finished when I return the addresses? How could I ensure that both functions finished before returning?

解决方案

Short answer: no. You will almost immediately get to the return line of myFunction, most likely well before the AJAX has completed.

Your best bet is to force geoCoder's requests to be synchronous. If you can't, then what you will need to do is set a global flag indicating when both requests have completed and wait for that flag to be set. If it is, then you can gather your data, construct your array, and return it.

function myFunction(marker1,marker2) {
  var firstAddress  = null;
  var secondAddress = null;

  document.geoCodeRequestCompleteFlag = 0;

  geocoder.geocode({'latLng': marker1.getPosition()}, function(results, status) {
    # if geocoding successful, set firstAddress
  })

  geocoder.geocode({'latLng': marker2.getPosition()}, function(results, status) {
    # if geocoding successful, set secondAddress
  })

  setTimeout( function( ) {
    document.geoCodeRequestCompleteFlag = -1;
  }, 15000 ); // -- ensure that we don't get stuck indefinitely

  while( document.geoCodeRequestCompleteFlag < 2 && document.geoCodeRequestCompleteFlag > 0 ) {
    // wait
  }

  if( document.geoCodeRequestCompleteFlag < 0 ) {
    return 'timeout';
  } else {
    return [firstAddress,secondAddress];
  }
}

Be sure to add a document.geoCodeRequestCompleteFlag++ inside your marker1.getPosition() and marker2.getPosition() callbacks.

The downside to this, is that if your requests take a long time, the browser may kill your script.

这篇关于函数在返回之前是否会等待异步函数完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆