New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
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
  • Socket score

Version published
Weekly downloads
2
decreased by-50%
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.

  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

Keywords

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc