gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:37:06 27 4
gpt4 key购买 nike

我如何使用 Observable.bindCallback()带有返回 2 个参数的回调,callback(results, status) ?这个例子是 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) 是从 next 中“抛出”错误的最佳实践处理程序并在 error 中捕获它处理程序,或者只调用方法 handleError()

2) 我得到 Cannot read property 'nearbySearch' of undefined(…)错误。但是当我调用 const nearbyAsObservable = 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<{}>

更新看起来这个 hack 会修复 TS 错误

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

但是next如果我给它一个 (result, status)=>void 处理程序会提示

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

最佳答案

答案如下:

1) 根据需要将作用域绑定(bind)到您的回调(见评论)

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

3) 在 Observable.bindCallback() 中使用 selector 函数将多个返回参数映射到 subscribe 函数的单个响应中,并抛出错误。 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)
)

关于angular - 你如何使用 Observable.bindCallback(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com