0

I'm trying to solve a problem related to refresh tokens. So basically, I have function that checks if somebody is logged in:

isLogged(): Observable<any> {
  const valid = isTokenValid();
  if (valid) return Observable.of(true);
  else { return this.refresh() }
}

Obviously, this function returns observable. Now I have refresh function:

refresh() {
  if (this.existingSubscription) return this.existingSubscription;
  this.existingSubscription = this.http.post(...
  return this.existingSubscription;
}

This generally works, BUT, this.existingSubscription.subscribe still executes request, so I get execution of 2 requests instead of 1. (1 promise, multiple subscribers).

E.g. When token is not valid and I send 5 requests at the same time, it sends 5 same request token requests instead of 1.

this.existingsSubscription.subscribe is executing code all over again instead of subscribing to the existing request that has already been made.

Am I doing something wrong?

Haris Bašić
  • 1,383
  • 2
  • 12
  • 21
  • Perhaps you you should elaborate on the actual code rather than simply listing ellipsis `...` parts. Right now it looks like the inner parts are actually subscribing ( just going by naming convention ), hence the pointer to possible duplicate. if you really think this is not the case then actually demonstrate otherwise. Doing so might just get you the help you want. – Neil Lunn May 11 '17 at 08:54
  • Edited my question. – Haris Bašić May 11 '17 at 09:03

0 Answers0