Skip to content

Commit c4a4602

Browse files
fix(useAsyncQueue): trigger onFinished when the last task is rejected (#5144)
Co-authored-by: Vida Xie <[email protected]>
1 parent 94918fd commit c4a4602

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/core/useAsyncQueue/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,21 @@ describe('useAsyncQueue', () => {
135135
expect(finalTaskSpy).not.toHaveBeenCalled()
136136
})
137137
})
138+
139+
it('should trigger onFinished when the last task is rejected', async () => {
140+
const p3 = () => {
141+
return new Promise((resolve, reject) => {
142+
setTimeout(() => {
143+
reject(new Error('reject'))
144+
}, 30)
145+
})
146+
}
147+
const onFinishedSpy = vi.fn()
148+
useAsyncQueue([p1, p2, p3], {
149+
onFinished: onFinishedSpy,
150+
})
151+
await vi.waitFor(() => {
152+
expect(onFinishedSpy).toHaveBeenCalledOnce()
153+
})
154+
})
138155
})

packages/core/useAsyncQueue/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export function useAsyncQueue<T extends any[], S = MapQueueTask<T>>(
127127

128128
updateResult(promiseState.rejected, e)
129129
onError()
130+
if (activeIndex.value === tasks.length - 1)
131+
onFinished()
130132
return e
131133
})
132134
}, Promise.resolve())

0 commit comments

Comments
 (0)