加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 你如何使用Observable.bindCallback()

发布时间:2020-12-17 07:15:15 所属栏目:安全 来源:网络整理
导读:如何将Observable.bindCallback()与返回2个args,回调(结果,状态)的回调一起使用?该示例使用以下google.maps.places API: const service = new google.maps.places.PlacesService(map); // service.nearbySearch(request,callback); function callback(resu
如何将Observable.bindCallback()与返回2个args,回调(结果,状态)的回调一起使用?该示例使用以下google.maps.places API:

const service = new google.maps.places.PlacesService(map);
  // service.nearbySearch(request,callback);


  function callback(results,status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
      }
    }
  }

我想做这样的事情:

const handleError = err=>console.error(error);

const nearbyAsObservable = Observable.bindCallback(service.nearbySearch)
nearbyAsObservable(request)
   .subscribe( 
     (results,status)=>{
       if (status!="OK") handleError(results);
       callback
     },handleError
   )

但我不确定以下几点:

1)是从下一个处理程序“抛出”错误并在错误处理程序中捕获它,或者只是调用方法handleError()的最佳实践?

2)我无法读取未定义(…)错误的属性’nearbySearch’.但是当我调用const nearAsObservable = Observable.bindCallback(service.nearbySearch.bind(service))时,我收到一个TS错误:

// const nearbyAsObservable = Observable.bindCallback(service.nearbySearch.bind(service) )
// nearbyAsObservable(request)
[ts] Supplied parameters do not match any signature of call target.
const nearbyAsObservable: () => Observable<{}>

更新它看起来像这个黑客将修复TS错误

const nearbyAsObservable : any = Observable.bindCallback(service.nearbySearch.bind(service) )
nearbyAsObservable(request)
   .subscribe( 
     (results,handleError
   )

但是如果我给它一个(结果,状态)=> void,下一个处理程序会抱怨

3)如何从Observable< [result,status]>转换Observable返回值?到Observable< PlaceResult []>?

解决方法

答案如下:

1)根据需要将范围绑定到您的回调(请参阅注释)

2)如果绑定范围,则使用let nearbyAsObservable:any;修复TS错误how do I use `Observable.bindCallback()` with typescript

3)使用Observable.bindCallback()中的选择器函数将多个返回参数映射到订阅函数的单个响应中,并且还抛出错误. How do I use the RXJS selector function in the Observable.bindCallback method?

let nearbyPlaces = function(position: google.maps.LatLng) : Observable<google.maps.places.PlaceResult[]> {  
  const service = new google.maps.places.PlacesService(map)
  // 1) bind scope
  const nearbySearchCallback = service.nearbySearch.bind(service)

  let nearbyAsObservable : any;
  // 2) type any fixes: 
  //    [ts] Supplied parameters do not match any signature of call target. 
  //    const nearbyAsObservable: () => Observable<{}>  

  nearbyAsObservable = Observable.bindCallback( 
    nearbySearchCallback        // with bound scope,(results,status) => {    // 3) selector function
        if (status != google.maps.places.PlacesServiceStatus.OK) throw {status,results};
        return results  
      }
  );
  const placeRequest = {
    location: position,radius: 25,rankBy: google.maps.places.RankBy.PROMINENCE,}
  return nearbyAsObservable(placeRequest) as Observable<google.maps.places.PlaceResult[]>
}


// usage example:
nearbyPlaces(position).subscribe(
  (results:google.maps.places.PlaceResult[])=>console.log(results),err=>console.error(err)
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读