异步等待一个函数被调用

问题描述

我在 WebKit (Android) 和 WKWebView (iOS) 中使用了 bing-maps JS 控件和 API。

var map = new Microsoft.Maps.Map(document.getElementById('myMap'),{});
// Binding the event
Microsoft.Maps.Events.addHandler(map,'viewchangeend',function () { console.log('mapViewchangeend'); });

代码的后面,我调用map.setView。因为这个方法是异步的,所以我需要等待事件处理程序调用上面的函数

function SetMapViewLocations(pointsToView) {
  try {
    if (pointsToView.length === 1) {
      map.setView({ center: pointsToView[0],zoom: 17 });
    } else {
      var locationRect = window.Microsoft.Maps.LocationRect.fromLocations(
        pointsToView
      );
      locationRect.height =
        locationRect.height < MIN_BOUNDS_SIZE
          ? MIN_BOUNDS_SIZE
          : locationRect.height;
      locationRect.width =
        locationRect.width < MIN_BOUNDS_SIZE
          ? MIN_BOUNDS_SIZE
          : locationRect.width;
      map.setView({ bounds: locationRect });
      // I need to wait without blocking for the viewchangeend associated method to be called before exiting the method.

    }
  } catch (exception) {
    SetJavascriptException(exception);
  }
}

在 c# 中,我会在回调处理程序方法中设置一个 AutoResetEvent 并在调用 map.setView(...) 后立即等待它。

我在 Javascript 中有哪些选项?

我看不出 Promises 在这里有什么帮助,因为我不知道如何在我的情况下构建一个

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)