RXJS 5 bindCallback when the callback is the first argument
javascript, observable, react-native, rxjs, rxjs5
Solution
Create a new function with rearranged arguments
const bound = Rx.Observable.bindCallback((options, cb) => {
if(typeof options === 'function') {
cb = options
options = null
}
navigator.geolocation.getCurrentPosition(cb, null, options)
})
Or use `bindNodeCallback` if you want to handle errors.
Problem
I want to convert a function: `static getCurrentPosition(geo_success, geo_error?, geo_options?)` to an observable. As you can see, the first argument to the function is the success callback. RxJS 5 bindCallback works on functions where the success callback is the last parameter. Is there a way to use bindCallback on `static getCurrentPosition(geo_success, geo_error?, geo_options?)` ? If not then I will manually convert the function to a `Promise` like number 2 here The function in question can be found here I would like to implement this as an observable: ``` navigator.geolocation.getCurrentPosition( (position) => { updateRegion(position) store.dispatch(getCurrentLocation(position)) }, error => Observable.of(getCurrentPositionRejected(error)), { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 } ) ``` My attempt at implementing the answer below: ``` class Vepo extends Component { componentDidMount() { const { store } = this.context this.unsubscribe = store.subscribe(() => { }) store.dispatch(fetchCategories()) store.dispatch(getCurrentPosition()) ************implementation starts here****************************** const bound = Observable.bindCallback((options, cb) => { if (typeof options === 'function') { cb = options options = null } navigator.geolocation.getCurrentPosition(cb, null, options) }) let x = bound(this.getPosition) x.subscribe( function (x) { console.log('Next: %s', x) }, function (err) { console.log('Error: %s', err) }, function () { console.log('Completed') }) x.onNext() } getPosition(position) { console.log(position) return position } ``` And it `console.log()`'s only from inside `getPosition()`