Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

javascript - 如何取消订阅retryWhen并在第一次api调用后捕获可观察的错误并修复延迟(How to unsubscribe from retryWhen and catch the error inside the observable and fix delay after first api call)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what do you thin can this help you?(您瘦了什么可以帮助您?)

you can have the property which shows is the request first(您可以拥有首先显示请求的属性) and depends of that you can change the delay miliseconds like this(并取决于您可以像这样更改延迟毫秒) and can you change your logic in your service(您可以改变服务逻辑吗) isFirstRequest = true; get data() { x => this.callAPI((x as any)).pipe( delay(isFirstRequest ? 0 : 2000), repeat(Infinity), map(response => { isFirstRequest = false; this.subject.next(response); return response; }), 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) } } ) ) ) ), takeWhile(response => /// you need to return some conditions) ) return this.subject; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...