tiny-async-task-queue
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

tiny-async-task-queue

npm module size

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.

  1. Supply an argument typed Function, invoke it when the task is finished.
  2. 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(() => {
        // do something...
        next()
    }, 500)
}

or this

function task() {
    return new Promise(resolve => {
        setTimeout(() => {
            // do something...
            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 // => this property pint to the `tiny-linked-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)

// console will logs as below:
// task1 running.
// task2 running.
// task3 running.

Tests

yarn test

Readme

Keywords

Package Sidebar

Install

npm i tiny-async-task-queue

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

5.32 kB

Total Files

5

Last publish

Collaborators

  • jinghua000