title: How to return an object in jsx and use in immediately with out depending on setState as its Asynchronous
body: <p>I am new to react and I am trying to do DRF (Django Rest Framework) with React so I am using the fetch() with required attributes to hit the API endpoint and getting some data. Now in my case, I need to hit two API endpoints. But I wanted to reuse the fetch() by putting it in a function which will return an object and I'll save using setState.</p>
  
  <p>My problem is the endpoint URL for the second API call is in the object returned by the first API call.</p>
  
  <p>Example:
  fetch(enpoint1, data) -> returns {name: 'something', url: 'endpoint2'}</p>
  
  <p>if I do setState here the object is not updated as setState is Asynchronous and so I cannot use it immediately.</p>
  
  <p>I might have a super silly mistake. Any advice on the "way I am doing it" or "on how to correct it" would be appreciated.</p>
  
  <pre><code>  componentDidMount() {
    let currentComponent = this;
    try {
      const data = {username: 'username', password: 'password'};
  //gets me the token
      fetch(URL(Just example), {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data)
      }).then(function(response) {
        return response.json();
      }).then(function(data){
        return data.token
      }).then(function(token){
        var project_object = currentComponent.fetchFunction(currentComponent.props.endpoint, token)
        console.log(project_object)
        //This is UNDEFINED
      })
    } catch (e) {
      console.log(e);
    }
  }
  </code></pre>
  
  <pre><code>fetchFunction(endPoint, token) {
      let currentComponent = this;
      var object = {}
      console.log(endPoint)
      try {
        //fetch for project object
        fetch(endPoint, {
          method: 'GET',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'authorization': 'Token ' + token
          }
        }).then(function(response) {
          return response.json();
        }).then(function(object){
          console.log(object)
          object = object
        })
      } catch (e) {
        console.log(e);
      }
      return object
  }
  </code></pre>