Skip to content

Fetching too quickly causes race conditions #302

Open
@seanyboy49

Description

@seanyboy49

Describe the bug
Invoking the get function too quickly in a short amount of time causes race conditions.

⚠️ Make a Codesandbox ⚠️
https://codesandbox.io/s/use-http-race-conditions-ecnqt?file=/src/App.js

To Reproduce
Steps to reproduce the behavior:

  1. Go to your developer console (cmd + option + i on chrome)
  2. Navigate to your network tab
  3. Simulate a slower connection speed by clicking the throttle tab and selecting fast 3g
  4. Click the button several times very quickly
  5. Observe the network requests. The UI will not reflect the latest request data

Expected behavior
I know this is a heavily contrived example, but it essentially mirrors the behavior I'm experiencing in a production app. A date picker has arrow buttons that allow the user to quickly change date query params that fire off a new useFetch request with each click. If the user clicks too quickly, then 90% of the time, I'll end up with UI that shows previous request data. After some amount of time (sometimes 30 seconds to a minute) the UI eventually updates to reflect the latest request data.

Note: I tried using the abort function exposed from useFetch to abort all previous requests, but this seems to abort only every other previous request or so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions