Search code examples
reactjsfetch

Return the fetch response from another file


I am trying to call a function that calls fetch to an API from a React component in a separate file and am not finding the correct solution to get the correct response back.

When I debug, the result returns before the updateAccount function has completed and the final result is never returned to my update function.

Inside the fetch, the API returns the correct response whether it is successful or has validation errors and those results are correctly assigned to result.success and result.errors but the result doesn't get returned from the function so that the caller can make use of those values.

Inside of my React component:

import { updateAccount } from '../services/requests';
...
const update = (account: EditAccountModel) => {
  const result = updateAccount(account);
  if(result.errors.length > 0) {
    // will notify of errors
    console.log(result.errors); // is an empty array instead of validation errors
  } else {
    // will notify of success
    console.log(result.success); // is an empty string instead of success message
  }
}
...

My request file

export const updateAccount = (account: EditAccountModel | undefined): EditAccountResponseModel => {
  const result = new EditAccountResponseModel();
  fetch(baseUrl, {
    method: 'PUT',
    body: JSON.stringify(account),
    headers 
  })
  .then(response => {
    if (!response.ok) {
      return Promise.reject(response);
    }
    result.success = `${account?.name} was updated successfully!`
  })
  .catch(error => {
    if (typeof error.json === "function") {
      error.json().then(jsonError => {
        result.errors.push(jsonError);
      }).catch(genericError => {
        result.errors.push(genericError);
      });
    }
  });
  return result;
}

Solution

  • The result reassignment happens inside then catch but it won’t be affective in the way you expected. The guaranteed way to return correct result is via a callback() passed to your updateAccount() if you could afford it:

    
    export const updateAccount = (
      account: EditAccountModel | undefined,
      callback: Function
    ): EditAccountResponseModel => {
    
      const result = new EditAccountResponseModel();
    
      fetch(baseUrl, {
        method: 'PUT',
        body: JSON.stringify(account),
        headers 
      })
      .then(response => {
        if (!response.ok) {
          return Promise.reject(response);
        }
    
        result.success = `${account?.name} was updated successfully!`
        callback(result);
      })
      .catch(error => {
        if (typeof error.json === "function") {
          error.json().then(jsonError => {
            result.errors.push(jsonError);
            callback(result);
          }).catch(genericError => {
            result.errors.push(genericError);
            callback(result);
          });
        }
        
    
      });
    }
    
    

    And inside your React component:

    const update = (account: EditAccountModel) => {
      const handleResult = (res) => {
        // your result callback code
        // ...
      };
    
      updateAccount(account, handleResult);
      // ...
    }
    

    Alternative way that keeps your current structure is to change your current updateAccount() to an async function, then return await fetch().