title: How to load a service in React before rendering components?
body: <p>If I have a service that calls some REST API resources, but in order to get the URL of that API need to perform another REST call locally from the same deployed site and read that URL from a config file.</p>
  
  <p>I made that call inside the constructor of the service, and set a local variable with the response of that call.</p>
  
  <p>The problem starts when other components try to call that service functions, the URL is still undefined at that point and the call fails. </p>
  
  <p>For example:</p>
  
  <p>Service</p>
  
  <pre><code>    let webServicesBaseUrl;
  
  class ApiService extends EventEmitter {
  
      // webServicesBaseUrl;
  
      constructor() {
          super();
  
          this.getWebServicesBaseUrl().then( (response) =&gt; {
              webServicesBaseUrl = response;
              console.log("..........................", webServicesBaseUrl)
          })
      }
  
   isAuthenticated(/*userName*/) {
          const url = new URL(webServicesBaseUrl + '/SessionManager/isAuthenticated');
          //     params = {user: userName}
          // Object.keys(params).forEach(key =&gt; url.searchParams.append(key, params[key]))
  
          return fetch(url,
              {
                  method: 'GET',
                  credentials: 'include',
                  // mode: "cors",
                  headers: headers
              }
          ).then((response) =&gt; response.text())
              .then((data) =&gt; {
                  return data;
              })
              .catch(console.log);
      }}
  
  export const service = new ApiService();
  </code></pre>
  
  <p>Component</p>
  
  <pre><code>import {service} from "../stores/RestService";
  componentWillMount() {
  
      service.isAuthenticated(/*this.props.user*/).then((response) =&gt; {
          if (response !== 'true') {
              this.props.history.push('/login')
          }
      });
  </code></pre>
  
  <p>Getting:</p>
  
  <p><a href="https://i.stack.imgur.com/s1DKc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s1DKc.png" alt="enter image description here"></a></p>