title: rxjs retryWhen promise is resolved
body: <p>I'm implementing an Angular service which calls a server for data. In every call I need to pass a token which lasts about a minute and then I need to map the response to get an specific field. So if my main call fails I need to call again for a token, wait for the response and then retry my first call, is there an easy way of doing this? Here are my two approaches (neither of them work propertly):</p>
  <pre><code>return this.http.post(url,
    firstCallText(this.token), { 
    responseType: 'text',
    headers
   })
    .pipe(
      map((xmlString: string) =&gt; {
        let asJson = this.xmlStringToJson(xmlString);
        return asJson[&quot;soap:Envelope&quot;][&quot;soap:Body&quot;][&quot;Response&quot;][&quot;#text&quot;];
      }),
      catchError(async err=&gt;{
        await this.http.post(url,
        getToken(),
        { 
          responseType: 'text',
          headers
        }).pipe(map((xmlString: string) =&gt; {
          let asJson = this.xmlStringToJson(xmlString);
          this.token = asJson[&quot;soap:Envelope&quot;][&quot;soap:Body&quot;][&quot;Response&quot;][&quot;Token&quot;][&quot;#text&quot;];
        })).toPromise()
        return EMPTY
      }),
      retry()
    )
  </code></pre>
  <p>This first method fails because the retry() gets called before the new token is received.
  My second approach:</p>
  <pre><code>return this.http.post(url,
    firstCallText(this.token), { 
    responseType: 'text',
    headers
   })
    .pipe(
      map((xmlString: string) =&gt; {
        let asJson = this.xmlStringToJson(xmlString);
        return asJson[&quot;soap:Envelope&quot;][&quot;soap:Body&quot;][&quot;Response&quot;][&quot;#text&quot;];
      }),
      retryWhen((errors) =&gt;{
         this.http.post(url,
          getToken(),
          { 
            responseType: 'text',
            headers
          }).pipe(map((xmlString: string) =&gt; {
            let asJson = this.xmlStringToJson(xmlString);
            this.token = asJson[&quot;soap:Envelope&quot;][&quot;soap:Body&quot;][&quot;Response&quot;][&quot;Token&quot;][&quot;#text&quot;];
          })).toPromise()
          return EMPTY          
      })
    )
  </code></pre>
  <p>The second one doesn´t retry correctly, I don't want to set a delay because the token call might be shorter or longer.</p>