Skip to content

Commit

Permalink
fix(useFetch): chain callbacks should be executed in order
Browse files Browse the repository at this point in the history
  • Loading branch information
younggglcy committed Sep 20, 2022
1 parent 402dce5 commit 553d9b4
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/core/useFetch/index.ts
Expand Up @@ -216,10 +216,11 @@ function headersToObject(headers: HeadersInit | undefined) {

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

2 comments on commit 553d9b4

@imaverickk
Copy link

@imaverickk imaverickk commented on 553d9b4 Sep 20, 2022

Choose a reason for hiding this comment

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

The callback for reduce should return a Promise, otherwise the prevCallback in the next call is undefined.

@younggglcy
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The callback for reduce should return a Promise, otherwise the prevCallback in the next call is undefined.

Yeah, that's right. I have no idea why the TS intellisense of my vscode changed the code to this. So I formatted my code manually and pushed a commit just now.

Please sign in to comment.