在promise函数的错误函数回调之后,附加对象的用法是什么

时间:2016-03-09 22:10:28

标签: javascript angularjs promise

在这个lib https://github.com/arunisrael/angularjs-geolocation/blob/master/src/geolocation.js#L39

中 用谷歌搜索整个网络,甚至是关于诺言的官方mozilla文档。没有结果。 我发现额外的选择让我感到困惑。额外opt的用法是什么?当我们真的需要设置它?

getLocation: function (opts) {
        var deferred = $q.defer();
        if ($window.navigator && $window.navigator.geolocation) {
          $window.navigator.geolocation.getCurrentPosition(function(position){
            $rootScope.$apply(function(){deferred.resolve(position);});
          }, function(error) {
            switch (error.code) {
              case 1:
                $rootScope.$broadcast('error',geolocation_msgs['errors.location.permissionDenied']);
                $rootScope.$apply(function() {
                  deferred.reject(geolocation_msgs['errors.location.permissionDenied']);
                });
                break;
              case 2:
                $rootScope.$broadcast('error',geolocation_msgs['errors.location.positionUnavailable']);
                $rootScope.$apply(function() {
                  deferred.reject(geolocation_msgs['errors.location.positionUnavailable']);
                });
                break;
              case 3:
                $rootScope.$broadcast('error',geolocation_msgs['errors.location.timeout']);
                $rootScope.$apply(function() {
                  deferred.reject(geolocation_msgs['errors.location.timeout']);
                });
                break;
            }
          }, opts);
        }
        else
        {
          $rootScope.$broadcast('error',geolocation_msgs['errors.location.unsupportedBrowser']);
          $rootScope.$apply(function(){deferred.reject(geolocation_msgs['errors.location.unsupportedBrowser']);});
        }
        return deferred.promise;
      }

1 个答案:

答案 0 :(得分:2)

这些opts Geolocation.getCurrentPosition()函数调用PositionOptions

你可以see documentation for this api here

您引用的图书馆只是一个好公民,允许某人直接通过opts来自顶级Geolocation方法的getLocation来电:

getLocation: function (opts) {

在包装现有API时,允许您的消费者通过选项通常很有帮助。这为来电者提供了更多选择(没有双关语)。