代码之家  ›  专栏  ›  技术社区  ›  Olivier MATROT

异步等待函数被调用

  •  0
  • Olivier MATROT  · 技术社区  · 4 年前

    我在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在这里会有什么帮助,因为我不知道如何在我的情况下构建一个Promises。

    0 回复  |  直到 4 年前