I want to delay after the api first api call not the first, now if waits two seconds then it make the first request, I want it to make the first request when I call next on my behaviour subject, then keep repeating with a delay.(我想在api第一个api调用之后而不是第一个api延迟,现在如果等待两秒钟,它将发出第一个请求,当我在我的行为主题上调用next时,我希望它发出第一个请求,然后一直重复执行。)
I can't unsubscribe when the retryWhen is executed.(当retryWhen执行时,我无法退订。)
I want to throw an error so I catch it inside the subscription and do something with it.(我想抛出一个错误,以便在订阅中捕获该错误并对其进行处理。) Or a way to find out that we are in the retryWhen(或找出我们正在重试的方法)
only works if we have some data back from the api, as soon a we enter the retry, I can't unsubscribe from the oberservable..(仅当我们从api返回一些数据时,此方法才有效,一旦我们进入重试,我便无法取消订阅该Oberservable。)
// Service
data = this.subject.pipe(
switchMap(
// call the api
x => this.callAPI((x as any))
.pipe(
delay(2000),
repeat(Infinity),
retryWhen(errors =>
errors
.pipe(
delayWhen(error => {
console.log(error)
if (error.status === 404) {
throwError(error)
return timer(1000 * 1)
} else if (error.status === 500) {
return timer(1000 * 5)
}
// don't retry not retry
else {
return throwError(error)
}
}
)
)
)
),
),
)
In my component I have(在我的组件中)
unsubscribe: Subject<void> = new Subject()
as soon as we enter the retry strategy, I want to know that so I do something with.(进入重试策略后,我想知道这一点,因此可以采取一些措施。) for now, I don't know and the old data from the observable is still valid so I can't render an error message to the user.(目前,我还不知道,可观察对象中的旧数据仍然有效,因此我无法向用户呈现错误消息。)
this.sub = this.service.data.pipe(
takeUntil(this.unsubscribe))
.subscribe(res => {
console.log(res)
},
x => {
// as soon as we enter the retry strategy, I want to know that so I do
something with.
console.log(x)
}
)
only works if we have some data back from the api, as soon a we enter the retry, I can't unsubscribe from the oberservable..(仅当我们从api返回一些数据时,此方法才有效,一旦我们进入重试,我便无法取消订阅该Oberservable。)
ngOnDestroy () {
console.log('component Destroyed, stop everything')
this.unsubscribe.next()
this.unsubscribe.complete()
ask by JS Lover translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…