🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

tiny-async-task-queue

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-async-task-queue

A tiny async task queue with one API.

1.0.1
latest
Source
npm
Version published
Weekly downloads
6
500%
Maintainers
1
Weekly downloads
 
Created
Source

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.

  • 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(() => {
        // 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

Keywords

async

FAQs

Package last updated on 26 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts