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?

CozyAzure
  • 8,280
  • 7
  • 34
  • 52
POV
  • 11,293
  • 34
  • 107
  • 201
  • 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. – dK- Sep 04 '18 at 00:16
  • How to use this right for request? – POV Sep 04 '18 at 00:24
  • I need to stop process when I get succesfull result from server – POV Sep 04 '18 at 00: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()` – dK- Sep 04 '18 at 00:48
  • More like you would need `retry` or `retryWhen` if dealing with errors/successful calls – CozyAzure Sep 04 '18 at 02:47

1 Answers1

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).

Hero Wanders
  • 3,237
  • 1
  • 10
  • 14
  • Can I use `filter` with condition inside to check response? And how to wait response from prev request and only then call next? – POV Sep 04 '18 at 00:44
  • 1
    Yes, the commented block is exactly the condition to check the response's success. Maybe retry (https://www.learnrxjs.io/operators/error_handling/retry.html) or retryWhen (https://www.learnrxjs.io/operators/error_handling/retrywhen.html) are more appropriate in your situation. Also have a look at this answer: https://stackoverflow.com/questions/44979131/rxjs-retry-with-delay-function – Hero Wanders Sep 04 '18 at 01:18
  • You know problem is that it will call next request not awaiting result from prev(fail or success) – POV Sep 04 '18 at 10:39
  • Thank you how to start countdown from 0 not from 3 second? – POV Sep 04 '18 at 11:58
  • Regarding the initial delay you want to avoid: have a look at this answer I found by googling: https://stackoverflow.com/questions/36612945/how-to-get-an-observable-to-return-data-immediately-and-every-5-seconds-thereaft – Hero Wanders Sep 04 '18 at 20:55