userlkjsflkdsvm
userlkjsflkdsvm

Reputation: 973

Getting set timeout value on authenticated endpoint

I am getting a numeric value for the seconds I need to set the set timeout interval. This interval will have multiple requests inside. I can't grab the value for the endpoint if there isn't a token bearing header. Which brings me to my problem. If I get a 401 error, the function stops entirely. How would I have the function keep going if it's a 401. I don't think setting a default interval would work because you would just be wrapping an interval in an interval. Any thoughts? Thanks. This is ionic 2+, basically angular 2+

app.component.ts

constructor(){
this.firstInterval()
}

firstInterval(){
    this.service.getSeconds().subscribe(result => {
      result.secondsbetween
      this.getGPSInterval(result.secondsbetween, 60000)
    })
  }    

  getGPSInterval(firstTime, secondTime){

   if(firstTime > 0){ 
    setInterval(() => {
        this.endpoint()
  },parseInt(firstTime + '000') )
    }
    else{
      setInterval(() => {

             this.endpoint()

       },secondTime)
    }
  }

Upvotes: 0

Views: 43

Answers (1)

Pengyy
Pengyy

Reputation: 38171

You can use finally operator which will still be fired even error happened. Mention that you will still need a default interval in order that this.service.getSeconds() fails.

defaultInterval = 1000;    // your real default interval value
realInterval = null;
this.service.getSeconds().finally(() => {             // finally operator
  this.getGPSInterval(this.defaultInterval, 60000);   // defaultInterval: error => 1000, success =>  real interval value from result.secondsbetween
}).subscribe(result => {
  this.defaultInterval = result.secondsbetween;       // replace with real interval value from result.secondsbetween
})

Plunker demo.

Upvotes: 1

Related Questions