0

In my code below I get a face from the camera every second. If I recognize the user, I get a token. If I don't recognize the user, I print an error.

The problem is that I don't want to spam the user with errors every second. How can I print the error on the nth consecutive failure?

subscription = getFaces()
.throttleTime(1000)
.switchMap(face => {
  return Observable.fromPromise(authenticateUserFace(face))
    .catch(err => {
      console.log('Not found')  // I want this to happen after 3 consecutive attempts
                                // But I don't want to retry
                                // I want a new value from the outer observable
      return Observable.empty()
    })
})
.subscribe((token) => {
  console.log('Found')
  console.log(token)
})

1 Answer 1

1

Just use a counter variable, and log to console only when it's a multiple of three:

const k = 3; // change this according to your needs
var counter = 0;

subscription = getFaces()
    .throttleTime(1000)
    .switchMap(face => {
        return Observable.fromPromise(authenticateUserFace(face))
            .catch(err => {
                counter++; // increment counter
                if (counter % k == 0) // this is true once every k times
                    console.log('Not found');

                return Observable.empty();
            })
    })
    .subscribe((token) => {
        console.log('Found');
        console.log(token);
    })
Sign up to request clarification or add additional context in comments.

Comments

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.