Skip to content
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

fix(useFetch): fix incorrect chainCallbacks behavior #2231

Merged
merged 5 commits into from Oct 5, 2022

Conversation

GODLiangCY
Copy link
Contributor

@GODLiangCY GODLiangCY commented Sep 20, 2022

Description

fixes #2225, #1310

Additional context

Like @imaverickk metioned in #2225, there's another solution: use Promise.all(). I'm not sure which one is better

Transforming for-await loops to the configured target environment ("es2017") is not supported yet. Thus Promise.all() seems to be the only solution?


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

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.

@imaverickk
Copy link

Promise.all() is parallel, and to ensure that the order of execution is predictable, it should probably look like this.

function chainCallbacks<T = any>(...callbacks: (((ctx: T) => void | Partial<T> | Promise<void | Partial<T>>) | undefined)[]) {
  return async (ctx: T) => {
    await callbacks.reduce((prevCallback, callback)=>{
      return prevCallback.then(async ()=>{
        if (callback)
          ctx = { ...ctx, ...(await callback(ctx)) }
      })
    }, Promise.resolve())
    return ctx
  }
}

@GODLiangCY
Copy link
Contributor Author

Promise.all() is parallel, and to ensure that the order of execution is predictable, it should probably look like this.

function chainCallbacks<T = any>(...callbacks: (((ctx: T) => void | Partial<T> | Promise<void | Partial<T>>) | undefined)[]) {
  return async (ctx: T) => {
    await callbacks.reduce((prevCallback, callback)=>{
      return prevCallback.then(async ()=>{
        if (callback)
          ctx = { ...ctx, ...(await callback(ctx)) }
      })
    }, Promise.resolve())
    return ctx
  }
}

Oh, you're right! And it makes sense that executing callback in order. Really appreciate for your review!

@webfansplz
Copy link
Member

Thanks for your contribution! Would you mind add a test cases for it ?

@GODLiangCY
Copy link
Contributor Author

Sure

Copy link
Member

@wheatjs wheatjs left a comment

Choose a reason for hiding this comment

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

Seem okay to me

@antfu antfu merged commit 457509a into vueuse:main Oct 5, 2022
@GODLiangCY GODLiangCY deleted the fix/useFetch branch October 7, 2022 16:33
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.

useFetch: chainCallbacks wrong use of forEach with async-await
5 participants