Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useAsyncQueue.md
1---2category: Utilities3---45# useAsyncQueue67Executes each asynchronous task sequentially and passes the current task result to the next task.89## Usage1011```ts12import { useAsyncQueue } from '@vueuse/core'1314function p1() {15return new Promise((resolve) => {16setTimeout(() => {17resolve(1000)18}, 10)19})20}2122function p2(result: number) {23return new Promise((resolve) => {24setTimeout(() => {25resolve(1000 + result)26}, 20)27})28}2930const { activeIndex, result } = useAsyncQueue([p1, p2])3132console.log(activeIndex.value) // current pending task index3334console.log(result) // the tasks result35```3637### Result State3839Each task in the result array has a `state` and `data` property:4041```ts42interface UseAsyncQueueResult<T> {43state: 'aborted' | 'fulfilled' | 'pending' | 'rejected'44data: T | null45}46```4748### Interrupt on Failure4950By default, if a task fails, subsequent tasks will not be executed. Set `interrupt: false` to continue executing even after failures.5152```ts53const { result } = useAsyncQueue([p1, p2], {54interrupt: false, // continue even if p1 fails55})56```5758### Callbacks5960```ts61const { result } = useAsyncQueue([p1, p2], {62onError() {63console.log('A task failed')64},65onFinished() {66console.log('All tasks completed (or interrupted)')67},68})69```7071### Abort Signal7273You can pass an `AbortSignal` to cancel the queue execution.7475```ts76const controller = new AbortController()7778const { result } = useAsyncQueue([p1, p2], {79signal: controller.signal,80})8182// Later, abort the queue83controller.abort()84```8586## Type Declarations8788```ts89export type UseAsyncQueueTask<T> = (...args: any[]) => T | Promise<T>90type MapQueueTask<T extends any[]> = {91[K in keyof T]: UseAsyncQueueTask<T[K]>92}93export interface UseAsyncQueueResult<T> {94state: "aborted" | "fulfilled" | "pending" | "rejected"95data: T | null96}97export interface UseAsyncQueueReturn<T> {98activeIndex: ShallowRef<number>99result: T100}101export interface UseAsyncQueueOptions {102/**103* Interrupt tasks when current task fails.104*105* @default true106*/107interrupt?: boolean108/**109* Trigger it when the tasks fails.110*111*/112onError?: () => void113/**114* Trigger it when the tasks ends.115*116*/117onFinished?: () => void118/**119* A AbortSignal that can be used to abort the task.120*/121signal?: AbortSignal122}123/**124* Asynchronous queue task controller.125*126* @see https://vueuse.org/useAsyncQueue127* @param tasks128* @param options129*/130export declare function useAsyncQueue<T extends any[], S = MapQueueTask<T>>(131tasks: S & Array<UseAsyncQueueTask<any>>,132options?: UseAsyncQueueOptions,133): UseAsyncQueueReturn<{134[P in keyof T]: UseAsyncQueueResult<T[P]>135}>136```137