Skip to content

useAsyncQueue

Category
Export Size
505 B
Last Changed
6 months ago

Executes each asynchronous task sequentially and passes the current task result to the next task

Demo

activeIndex: 1
result: [ { "state": "fulfilled", "data": 1000 }, { "state": "fulfilled", "data": 2000 } ]

Usage

ts
import { 
useAsyncQueue
} from '@vueuse/core'
function
p1
() {
return new
Promise
((
resolve
) => {
setTimeout
(() => {
resolve
(1000)
}, 10) }) } function
p2
(
result
: number) {
return new
Promise
((
resolve
) => {
setTimeout
(() => {
resolve
(1000 +
result
)
}, 20) }) } const {
activeIndex
,
result
} =
useAsyncQueue
([
p1
,
p2
])
console
.
log
(
activeIndex
.
value
) // current pending task index
console
.
log
(
result
) // the tasks result
js
import { useAsyncQueue } from '@vueuse/core'
function p1() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(1000)
    }, 10)
  })
}
function p2(result) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(1000 + result)
    }, 20)
  })
}
const { activeIndex, result } = useAsyncQueue([p1, p2])
console.log(activeIndex.value) // current pending task index
console.log(result) // the tasks result

Type Declarations

Show Type Declarations
ts
export type 
UseAsyncQueueTask
<
T
> = (...
args
: any[]) =>
T
|
Promise
<
T
>
type
MapQueueTask
<
T
extends any[]> = {
[
K
in keyof
T
]:
UseAsyncQueueTask
<
T
[
K
]>
} export interface
UseAsyncQueueResult
<
T
> {
state
: "aborted" | "fulfilled" | "pending" | "rejected"
data
:
T
| null
} export interface
UseAsyncQueueReturn
<
T
> {
activeIndex
:
ShallowRef
<number>
result
:
T
} export interface UseAsyncQueueOptions { /** * Interrupt tasks when current task fails. * * @default true */
interrupt
?: boolean
/** * Trigger it when the tasks fails. * */
onError
?: () => void
/** * Trigger it when the tasks ends. * */
onFinished
?: () => void
/** * A AbortSignal that can be used to abort the task. */
signal
?: AbortSignal
} /** * Asynchronous queue task controller. * * @see https://vueuse.org/useAsyncQueue * @param tasks * @param options */ export declare function
useAsyncQueue
<
T
extends any[],
S
=
MapQueueTask
<
T
>>(
tasks
:
S
&
Array
<
UseAsyncQueueTask
<any>>,
options
?: UseAsyncQueueOptions,
):
UseAsyncQueueReturn
<{
[
P
in keyof
T
]:
UseAsyncQueueResult
<
T
[
P
]>
}>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
Robin
ethansnow2012
cross-origin
Okoro Redemption
donotloveshampo
Yugang Cao
webfansplz

Changelog

v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.4.0 on
22e26 - feat: support in infinite tasks in typescript (#3333)
v10.2.0 on
7da7c - feat: add options.signal parameter (#3033)

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access