Ben*_*ins 3 javascript observable rxjs react-native rxjs5
我想转换一个函数:
static getCurrentPosition(geo_success, geo_error?, geo_options?)
一个可观察的.
如您所见,该函数的第一个参数是成功回调.
RxJS 5 bindCallback适用于成功回调是最后一个参数的函数.
有没有办法使用的方式bindCallback上
static getCurrentPosition(geo_success, geo_error?, geo_options?)?
如果没有,那么我将在这里手动将函数转换为Promise类似的数字2
有问题的功能可以在这里找到
我想将其作为一个可观察的实现:
navigator.geolocation.getCurrentPosition(
(position) => {
updateRegion(position)
store.dispatch(getCurrentLocation(position))
},
error => Observable.of(getCurrentPositionRejected(error)),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
)
Run Code Online (Sandbox Code Playgroud)
我试图实现以下答案:
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
}
Run Code Online (Sandbox Code Playgroud)
它console.log()"从内只发getPosition()
使用重新排列的参数创建一个新函数
const bound = Rx.Observable.bindCallback((options, cb) => {
if(typeof options === 'function') {
cb = options
options = null
}
navigator.geolocation.getCurrentPosition(cb, null, options)
})
Run Code Online (Sandbox Code Playgroud)
或者bindNodeCallback如果您想处理错误,请使用.
| 归档时间: |
|
| 查看次数: |
940 次 |
| 最近记录: |