1

Is it correct way to use flatMap?

const observer = Observable
    .interval(3000)
    .takeUntil(Observable.timer(10000))
    .flatMap(this.askToReadyRecordVideo);

private askToReadyRecordVideo(): Observable<any> {
    return this.requestMethods.askToReadyRecordVideo({});
}

In this line I tied to send request to server each 3 seconds until 10 seconds then call method this.askToReadyRecordVideo() that returns data from server.

I finish this when I get successfull response. Is it true?

5
  • 1
    This should do what you are set up to do. except it does not stop after the get successful response. It will only stop on the 10th second. Commented Sep 4, 2018 at 0:16
  • How to use this right for request? Commented Sep 4, 2018 at 0:24
  • I need to stop process when I get succesfull result from server Commented Sep 4, 2018 at 0:27
  • So, it's like 10 secs or success? 10 secs has been done. IMHO, stop process on success should be the job of the subscriber. observer.takeUntil(res=>!res.success)..subscribe() Commented Sep 4, 2018 at 0:48
  • More like you would need retry or retryWhen if dealing with errors/successful calls Commented Sep 4, 2018 at 2:47

1 Answer 1

1

I'm not sure whether I understand you correctly, but your code does the following: The method askToReadyRecordVideo will get called every 3 seconds until 10 seconds are over (there will be three calls, at 3s, 6s and 9s). Your observable observer will emit the results of those server calls.

If you want to cancel the process after your first successful response add the following:

.filter(resp => /* return true when resp indicates success */)
.take(1)

If every answer is a success (i.e. errors are indicated by an error event pushed through the observable), then just omit the filter line.

By the way: Be careful when passing callbacks to avoid surprises about what this means in askToReadyRecordVideos. You may use flatMap(() => this.askToReadyRecordVideo()) or flatMap(this.askToReadyRecordVideo.bind(this)) instead of flatMap(this.askToReadyRecordVideo).

Sign up to request clarification or add additional context in comments.

5 Comments

Can I use filter with condition inside to check response? And how to wait response from prev request and only then call next?
Yes, the commented block is exactly the condition to check the response's success. Maybe retry (learnrxjs.io/operators/error_handling/retry.html) or retryWhen (learnrxjs.io/operators/error_handling/retrywhen.html) are more appropriate in your situation. Also have a look at this answer: stackoverflow.com/questions/44979131/…
You know problem is that it will call next request not awaiting result from prev(fail or success)
Thank you how to start countdown from 0 not from 3 second?
Regarding the initial delay you want to avoid: have a look at this answer I found by googling: stackoverflow.com/questions/36612945/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.