Simple Async Queue for async/await.
yarn add @kkdy/queue
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()
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()
const result: number[] = []
const tasks = expected.map((e) => () => {
result.push(e)
return Promise.resolve()
})
await Queue.Promise(3, {
queue: tasks,
})
// try/catch
try {
await q.allDone()
} catch(err) {
// todo
}
// promise
Queue.Promise(3, {
queue: tasks,
})
.catch(err => {})
Queue.of(10, {
onError: (err) => {},
finalTask: () => {},
queue: tasks
})