title: How to make common API call function using fetch
body: <p>I am trying to make common function which will handle all of my API calls from anywhere</p>
  
  <p>I am using react": "^16.8.6" and fetch for making api call</p>
  
  <p>So far what i have figure out to do 
  is </p>
  
  <p>Helper.js</p>
  
  <pre><code>export function ApiHelper(url, data = {}, method = 'POST') {
      let bearer = 'Bearer ' + localStorage.getItem('user_token');
      var promise = fetch(url, {
          method: method,
          withCredentials: true,
          // credentials: 'include',
          headers: {
              'Authorization': bearer,
              'X-FP-API-KEY': 'chaptoken', 
              'Content-Type': 'application/json'
          }
      })
      .then(res =&gt; res.json())
      .then(
          (result) =&gt; {
              console.log(result);
          },
          (error) =&gt; {
              error = error;
          }
      )
  }
  
  export function AnyOtherHelper() {
      return 'i am from helper function';
  }
  </code></pre>
  
  <p>And here is from where i am calling this function </p>
  
  <pre><code>componentDidMount() {
      let url = `http://localhost/project/api/getdata`;
      let op = ApiHelper(url);
  }
  </code></pre>
  
  <p>when I console result in <code>then</code> i am getting appropriate result but what i want to return that response how can i do this part is troubling me
  Even i have try to store the result in global variable and it is not working.
  Also i have to return the response only when promise is resolved.</p>