Skip to content

isLoading flag is false for a pending request that follows cancelled request #106

@suhailk4

Description

@suhailk4

🐞 Bug Report

Describe the bug

IsLoading flag in pending state is false.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. use a useFetchye hook and defer it for later use
  2. In the useEffect run the returned fn from useFetchye to call the API - here loading flag is true
  3. Cancel the request using controller.abort
  4. re-run(call) the api again, this time isLoading flag is false when it should be true.

Additional info:

Create a simple component and use useFetchye with defer in that and try to use below code

 const getData = (url, controller ) => async (fetchClient, requestPayload, options) => {
  let payload;
  let error;
  try {
    const res = await fetchClient(url, {
      ...options,
      method: 'POST',
      signal: controller.signal,
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(requestPayload),
    });
    const {
      data,
    } = await res.json();
    payload = {
      data,
    };
  } catch (e) {
    error = e;
  }
  return {
    payload,
    error,
  };
};
  useEffect(() => {
            if (searchPayload) {
            runAPI().then((response) => {
              if (response?.error?.name === 'AbortError') {
                return;
              }
              if (response.error || !blockResponse?.data?.data) {
                // show error message
              } else {
                // logic here
              }
            });
          }
            return () => {
            controller.abort();
          };
  }, [searchPayload]);

/// Dummy component code

const onSearch = () => {

setSearchPayload([{bid: 1212, isAvailable: true}])
}

const [searchPayload, setSearchPayload] = useState(null);

<button type="button" onClick={onSearch}></button>

Expected behavior

isLoadingFlag should be true everytime request is in pending state

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: [e.g. macOS, Windows]
  • Browser (if applies) [e.g. chrome, safari]
  • Version of fetchye: [e.g. 5.0.0]
  • Node version:[e.g 10.15.1]

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions