Skip to content

fix(useLoading): stay loading until every overlapping promise settles - #577

Merged
hyesungoh merged 2 commits into
toss:mainfrom
rjsgud49:fix/use-loading-overlapping
Oct 27, 2026
Merged

hyesungoh merged 2 commits into
toss:mainfrom
rjsgud49:fix/use-loading-overlapping

Conversation

@rjsgud49

Copy link
Copy Markdown
Contributor

Overview

When startLoading is called again before the previous promise settles, loading goes back to false as soon as the first promise settles, even though the second one is still pending.

const [loading, startLoading] = useLoading();

startLoading(fetchA()); // settles after 1s
startLoading(fetchB()); // settles after 3s
// main: loading is false from 1s to 3s while fetchB is still running

This shows up with things like search-as-you-type or a retry button: the spinner disappears (or a disabled={loading} button is enabled again) while a request is still in flight.

Fix: the hook counts the promises that haven't settled yet in a ref, and only sets loading to false when the count gets back to 0. A single call behaves as before, and the unmount check is unchanged.

Tests added:

  • two overlapping promises: loading stays true after the first resolves and becomes false after the second (fails on main)
  • same with the first one rejecting (fails on main)

No public API change, so I left the JSDoc and docs as they are. Added a patch changeset.

Checklist

  • Did you write the test code?
  • Have you run yarn run fix to format and lint the code and docs? (ran yarn lint, yarn typecheck and prettier --list-different on the changed files; no new warnings)
  • Have you run yarn run test:coverage to make sure there is no uncovered line? (100%; test:compiled also passes)
  • Did you write the JSDoc? (no public API change, JSDoc unchanged)

@changeset-bot

changeset-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0a1458e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-simplikit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov-commenter

codecov-commenter commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (402995e) to head (0a1458e).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #577   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           70        70           
  Lines         2288      2291    +3     
  Branches       738       739    +1     
=========================================
+ Hits          2288      2291    +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hyesungoh hyesungoh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its great! 👍 thank you

@hyesungoh
hyesungoh merged commit 89ad72e into toss:main Oct 27, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants