-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(useAsyncState): cancel previous promise and use latest promise #4936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@vueuse/components
@vueuse/core
@vueuse/electron
@vueuse/firebase
@vueuse/integrations
@vueuse/math
@vueuse/metadata
@vueuse/nuxt
@vueuse/router
@vueuse/rxjs
@vueuse/shared
commit: |
const rawPromise = _promise | ||
_promise = new Promise((resolve, reject) => { | ||
cancel() | ||
cancel = () => (resolve = reject = noop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this doesn't actually cancel the previous promise; it still continues executing in the background, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this doesn't actually cancel the previous promise; it still continues executing in the background, right?
Thanks for the review 🙏 — you're totally right, the previous promise isn't truly cancelled; it still runs in the background.
What the code does is invalidate the resolve and reject of the previous promise whenever execute is called, so that only the latest one takes effect.
This way, we avoid having any result from the old promise interfere with the new one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could look into AbortController
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can wait for nuxt/nuxt#32531 and adapt some changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could look into AbortController
Got it, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can wait for nuxt/nuxt#32531 and adapt some changes
I see see. Thanks for the reply
Before submitting the PR, please make sure you do the following
fixes #123
).Description
This PR fixes an issue where, due to network delays, older requests could overwrite the latest one — like when switching tabs quickly. It now ensures only the latest result is used, and also adds support for manual cancellation.
Additional context
Replaces #4797 — reopened because the previous branch name conflicted with an existing one, and includes updated test cases and add doc.