Skip to content

Latest commit

 

History

History
98 lines (70 loc) · 1.41 KB

README.md

File metadata and controls

98 lines (70 loc) · 1.41 KB

queue

npm (tag) Codecov npm bundle size (version)

Simple Async Queue for async/await.

Install

yarn add @kkdy/queue

Usage

Basic

import { Queue } from '@kkdy/queue'

// set concurrency = 10, default is 1
const q = Queue.of(10)

const expected = [1, 2, 3]
const result: number[] = []

expected.forEach((e) => {
  // pushTask will auto start the queue
  q.pushTask(() => {
    result.push(e)
    return Promise.resolve()
  })
})

await q.allDone()

Init with an array of tasks

const result: number[] = []

const tasks = expected.map((e) => () => {
  result.push(e)
  return Promise.resolve()
})

const q = Queue.of(10, { queue: tasks })

// begin the queue
q.next()

Use as a Promise

const result: number[] = []
const tasks = expected.map((e) => () => {
  result.push(e)
  return Promise.resolve()
})

await Queue.Promise(3, {
  queue: tasks,
})

Catch error

// try/catch
try {
  await q.allDone()
} catch(err) {
  // todo
}

// promise

Queue.Promise(3, {
  queue: tasks,
})
  .catch(err => {})

Use with hooks

Queue.of(10, {
  onError: (err) => {},
  finalTask: () => {},
  queue: tasks
})