【问题标题】:Ionic 3 unable to reject promise in a callback functionIonic 3 无法在回调函数中拒绝承诺
【发布时间】:2018-03-27 03:28:28
【问题描述】:

我在 Ionic 3 中编写了一个函数来获取用户的位置并使用用户的经度和纬度获取其地址。此功能还检查用户的位置设置是否启用,如果没有,它将调用this.diagnostic.switchToLocationSettings() 它基本上显示位置设置供用户打开。我还添加了this.diagnostic.registerLocationStateChangeHandler 方法来检查用户是否真的在我之前显示的位置设置中打开了位置。如果一切正常,我将继续运行其他流程,例如获取地理位置并转换为地址。

下面是我的完整代码:

  getMyLocation() {
    this.geoOptions = {
      enableHighAccuracy: false
    }
    const loadingMsg = this.loadingCtrl.create({
      content: "Getting your location..."
    })

    loadingMsg.present();



    this.diagnostic.isLocationEnabled().then(test => {
        this.diagnostic.registerLocationStateChangeHandler((state)=>{
        if((this.platform.is('ios')) && (state !== this.diagnostic.permissionStatus.GRANTED ||
           state !== this.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE) ||
           (this.platform.is('android')) && (state === this.diagnostic.locationMode.LOCATION_OFF )){
            return Promise.reject("Please switch on your location");
          }
       })
      if (!test) {
        this.diagnostic.switchToLocationSettings()
      }

      // return this.geolocation.getCurrentPosition(this.geoOptions)
      return this.geolocation.getCurrentPosition(this.geoOptions)
      // console.log("Error! Please switch on your location");
      // return Promise.reject("Error! Please switch on your location");
    },(err:any)=>{
      loadingMsg.dismiss();
      console.log("error : " + err);

    }).then((pos: Geoposition) =>{

      return this.nativeGeocoder.reverseGeocode(pos.coords.latitude, pos.coords.longitude);

    },(err: PositionError)=>{
      loadingMsg.dismiss();
      console.log("error : " + err.message);
      return Promise.reject(err.message);

    }).then((result: NativeGeocoderReverseResult) =>{

      this.currentAddress = result.locality+", "+result.countryName;
      loadingMsg.dismiss();

    },(err:any)=>{

      console.log("error : " + err);
      loadingMsg.dismiss();
      return Promise.reject(err);

    })
  }

问题是如果在this.diagnostic.switchToLocationSettings() 被解雇后用户没有打开,我无法停止承诺链接。我试图停止链接的地方是在this.diagnostic.registerLocationStateChangeHandler 回调函数中。我是初学者承诺链式方法。

【问题讨论】:

    标签: angular typescript promise ionic3


    【解决方案1】:

    你可以这样做:

    您可以在需要abort promise chain的地方使用throw new Error('abort promise chain');return null;

     this.diagnostic.isLocationEnabled().then(test => {
            this.diagnostic.registerLocationStateChangeHandler((state)=>{
            if((this.platform.is('ios')) && (state !== this.diagnostic.permissionStatus.GRANTED ||
               state !== this.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE) ||
               (this.platform.is('android')) && (state === this.diagnostic.locationMode.LOCATION_OFF )){
                throw new Error('abort promise chain');//here
                return null;
              }
           })
    

    【讨论】:

    • 谢谢!,我现在设法让它工作,虽然我在我的代码中发现了几个问题......所以为了简单起见,我删除了 this.diagnostic.registerLocationStateChangeHandler 并将 throw new Error('abort promise chain'); 添加到 if(!test)陈述。然后我发现,如果我的承诺链中有错误,我不必为所有 then(success,err) 创建错误处理程序,而只需在最后一个链中创建它或将 .catch(err) 放入它。
    • 好的太好了!在这里您可以找到更多选项:github.com/petkaantonov/bluebird/issues/581
    • 我看到有更多的方法可以使用 bluebird 来处理 promise,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多