How do I use Observable.bindCallback()
with a callback that returns 2 args, callback(results, status)
? The example is with google.maps.places
API below:
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]);
}
}
}
I want to do something like this:
const handleError = err=>console.error(error);
const nearbyAsObservable = Observable.bindCallback(service.nearbySearch)
nearbyAsObservable(request)
.subscribe(
(results,status)=>{
if (status!="OK") handleError(results);
callback
}
, handleError
)
but I am unsure about the following:
1) is the best practice to "throw" an error from the next
handler and catch it in the error
handler, or just call the method handleError()
?
2) I am getting Cannot read property 'nearbySearch' of undefined(…)
error. But when I call const nearbyAsObservable = Observable.bindCallback( service.nearbySearch.bind(service) )
I get a TS error:
// const nearbyAsObservable = Observable.bindCallback(service.nearbySearch.bind(service) )
// nearbyAsObservable(request)
[ts] Supplied parameters do not match any signature of call target.
const nearbyAsObservable: () => Observable<{}>
update it looks like this hack will fix the TS error
const nearbyAsObservable : any = Observable.bindCallback(service.nearbySearch.bind(service) )
nearbyAsObservable(request)
.subscribe(
(results,status)=>{
if (status!="OK") handleError(results);
callback
}
, handleError
)
but the next
handler complains if I give it a (result, status)=>void
3) how do I transform the Observable return from Observable<[result, status]>
to Observable<PlaceResult[]>
?