如何在rxjs中实现服务器轮询
我有一个非常简单的服务器轮询场景:如何在rxjs中实现服务器轮询,rxjs,reactive-programming,reactive-extensions-js,Rxjs,Reactive Programming,Reactive Extensions Js,我有一个非常简单的服务器轮询场景: 调用API->2。成功->3。等待500毫秒->4秒。返回到步骤1 及 调用API->2。onError->3。完成 我想使用rxjs,因为我已经在使用rxjava了。但我似乎无法找到解决我问题的正确方法。 我尝试过计时器和间隔,但问题是它们只是无限运行,没有处理程序在等待服务器响应或在发生错误时完全退出时暂停它们。尝试使用retryWhen,但根本无法使其工作 这就是我想要的: downloadData() { console.log('downlo
downloadData() {
console.log('downloading data')
$.getJSON('http://localhost:80')
.done((data) => {
console.log('done' + JSON.stringify(data))
setTimeout(() => { this.downloadData() }, 500)
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log(`Error: ${textStatus}`)
})
}
如何在rxjs中实现同样的功能?您应该看看
repeat
操作符:
fromFetch.fromFetch(`https://www.googleapis.com/books/v1/volumes?q=purple cow&maxResults=3`).pipe(
exhaustMap(response => {
if (response.ok) {
// OK return data
return response.json()
} else {
// Server is returning a status requiring the client to try something else.
return of({ error: true, message: `Error ${response.status}` })
}
}),
catchError(err => {
// Network or other error, handle appropriately
return of({ error: true, message: err.message })
}),
filter(resp => !resp.error)
delay(500),
repeat(),
).subscribe()