tiny-async-task-queue

Introduction
Here is a tiny async task queue, for several standalone task executing in order, with just one API.
Usage
npm i -S tiny-async-task-queue
const Queue = require('tiny-async-task-queue')
const queue = new Queue()
And the only API is add
.
queue.add(task1)
queue.add(task2)
queue.add(task3)
task1
, task2
, task3
, will executing one by one.
Tasks need according with ONE OF the rules below.
- Supply an argument typed
Function
, invoke it when the task is finished. - Return a
Promise
.
If your code satisfied with two rules, first finished rule will be triggered, and the second will be ignored.
For example:
your task can write like this
function task(next) {
setTimeout(() => {
next()
}, 500)
}
or this
function task() {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, 500)
})
}
If the task added to the empty queue, it will run immediately, otherwise it will be executed after other tasks finished.
And the internal queue based on tiny-linked-queue
, if you want to manipulate the internal queue you can use the queue
property.
const queue = new Queue()
queue.queue
Full demo
function task1(next) {
setTimeout(() => {
console.log('task1 running.')
next()
}, 100)
}
function task2() {
return new Promise(resolve => {
setTimeout(() => {
console.log('task2 running.')
resolve()
}, 50)
})
}
function task3() {
console.log('task3 running.')
}
const Queue = require('tiny-async-task-queue')
const queue = new Queue()
queue.add(task1)
queue.add(task2)
queue.add(task3)
Tests
yarn test