Skip to content

feat(useStorageAsync): add onReady option and Promise return #4158

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

Merged
merged 13 commits into from
Jul 28, 2025

Conversation

asika32764
Copy link
Contributor

@asika32764 asika32764 commented Aug 17, 2024

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.
⚠️ Slowing down new functions

Warning: Slowing down new functions

As the VueUse audience continues to grow, we have been inundated with an overwhelming number of feature requests and pull requests. As a result, maintaining the project has become increasingly challenging and has stretched our capacity to its limits. As such, in the near future, we may need to slow down our acceptance of new features and prioritize the stability and quality of existing functions. Please note that new features for VueUse may not be accepted at this time. If you have any new ideas, we suggest that you first incorporate them into your own codebase, iterate on them to suit your needs, and assess their generalizability. If you strongly believe that your ideas are beneficial to the community, you may submit a pull request along with your use cases, and we would be happy to review and discuss them. Thank you for your understanding.


Description

This PR is aim to solve the issue #2895

Currently, useStorageAsync is impossible to wait the first value loaded, sometimes when app initializing, if we must use the storage, we may get the default initial value since the async storage not prepared.

This PR add an onLoad event to let developer can solve this problem by manually resolve promises in this callback.

Additional context

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. new function labels Aug 17, 2024
ferferga
ferferga previously approved these changes Jan 21, 2025
Copy link
Member

@ferferga ferferga left a comment

Choose a reason for hiding this comment

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

LGTM, but honestly, for the given use case, why not use the sync useStorage? I'm approving this though since other functions (like useWebSocket) supports similar callbacks as well.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 21, 2025
@asika32764
Copy link
Contributor Author

For example, the capacitorStorage is asynchronous.

And sometimes people may write their own storage adapter since useStorageAsync and useStorage allows custom storage interface as an argument.

@dosubot dosubot bot removed the lgtm This PR has been approved by a maintainer label Jan 21, 2025
Co-authored-by: Robin <robin.kehl@singular-it.de>
@OrbisK OrbisK changed the title feat(useStorageAsync): add onLoad option feat(useStorageAsync): add onReady option Jan 22, 2025
asika32764 and others added 2 commits January 23, 2025 12:05
Co-authored-by: Robin <robin.kehl@singular-it.de>
Co-authored-by: Robin <robin.kehl@singular-it.de>
OrbisK
OrbisK previously approved these changes Jan 23, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 23, 2025
OrbisK
OrbisK previously approved these changes Jan 23, 2025
@OrbisK OrbisK requested a review from ferferga January 23, 2025 07:04
@antfu
Copy link
Member

antfu commented Feb 12, 2025

I feel that exposing a then function to the ref might be more intuitive where users could also await for it.

It can be both:

const accessToken = await useStorageAsync('access.token', '', SomeAsyncStorage)

or

const accessToken = useStorageAsync('access.token', '', SomeAsyncStorage)

accessToken.then(() => {
  // ...
})

@asika32764
Copy link
Contributor Author

Maybe

accessToken.ready.then()

Some libraries use this way.

@asika32764
Copy link
Contributor Author

So is there any update? Do we need to change to the new pattern?

@asika32764
Copy link
Contributor Author

asika32764 commented Jun 25, 2025

@antfu @OrbisK Sorry to bother but I think this function is required for peoples since there are some issues discussion about this feature.

If wee need more changes, please leave some messages or a final decision so that I can modify the code.

Currently there has no any workaround to solve the async initialize issue without this PR.

@ilyaliao
Copy link
Member

ilyaliao commented Jun 26, 2025

I feel that exposing a then function to the ref might be more intuitive where users could also await for it.

It can be both:

const accessToken = await useStorageAsync('access.token', '', SomeAsyncStorage)

or

const accessToken = useStorageAsync('access.token', '', SomeAsyncStorage)

accessToken.then(() => {
  // ...
})

It feels like using useFetch

const { isFetching, error, data } = await useFetch(url)

@asika32764 Sorry for the late reply. Would you be open to exploring how we could support something like this?

@asika32764
Copy link
Contributor Author

asika32764 commented Jun 27, 2025

I implemented the Promise to the return value. But I still keep onReady options if someone needs it.

const accessToken = useStorageAsync('access.token', '', SomeAsyncStorage, {
  onReady(value) {
    // ...
  }
})

accessToken.value // Origin way, may be empty if storage not prepared

const resolvedToken = await accessToken

resolvedToken.value // Resolved, still needs a .value to get value

@asika32764 asika32764 changed the title feat(useStorageAsync): add onReady option feat(useStorageAsync): add onReady option and Promise return Jun 27, 2025
@antfu antfu enabled auto-merge July 28, 2025 04:34
@antfu antfu added this pull request to the merge queue Jul 28, 2025
Merged via the queue into vueuse:main with commit 3a2df2e Jul 28, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants