singleFlight()
Prevent repeat submissions while saving.
Try the example
Click Save preferences several times during the one-second request. The button stays available so you can test repeated input, but the request counter increases only once.
Repeated clicks share one pending save.
Ready to save.
Add it to your project
- Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
- Connect the behavior. Copy the JavaScript tab and call its exported
mount(element)after your component’s elements exist. Replace simulated delays and data operations with your own application code. - Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.
API and lifecycle
Wrap an async task and call run(...args). While a call is pending, further activations return status busy instead of starting another operation. busy exposes the current in-flight state.
- Interface
task; run, busy; rejects on failure- Lifecycle
- A successful result contains status success and value. Task failures reject, so handle them with try/catch. busy resets in finally after either outcome.
Accessibility and safety
Implementation notes
Client protection is not server-side idempotency.
This prevents concurrent client calls only. Critical writes still need server-side idempotency. Keep disabled or busy controls understandable to keyboard and screen-reader users.
What to check
Activate repeatedly while saving and confirm one task starts. Reject the task and verify an error appears and a later retry can run.