@coolgk/queue

This is a super lightweight function that limits the number of async functions run concurrently and run them in order.

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
214May 9, 2021Nov 18, 2017Minified + gzip package size for @coolgk/queue in KB

Readme

@coolgk/queue

a javascript / typescript module

npm install @coolgk/queue

This is a super lightweight function that limits the number of async functions run concurrently and run them in order.

Report bugs here: https://github.com/coolgk/node-utils/issues

  1. Put async functions in a queue and limit the number of async functions that run concurrently.
  2. Run async functions in order
  3. Run x number of functions in parallel per batch in order. similar to async / await when the second parameter is 1.

    Examples

    ```javascript import { queue } from '@coolgk/queue'; // OR // const { queue } = require('@coolgk/queue');

function a (x) { console.log('start a'); return new Promise((resolve) => setTimeout(() => { console.log('end a', x); resolve('a') }, 1300)); }

function b (x) { console.log('start b'); return new Promise((resolve) => setTimeout(() => { console.log('end b', x); resolve('b') }, 1200)); }

function c (x) { console.log('start c'); return new Promise((resolve) => setTimeout(() => { console.log('end c', x); resolve('c') }, 100)); }

// call a, b, c in order i.e. b does not start until a resolves queue(a); queue(b); queue(c);

// call a 5 times, each waits until the previous call resolves [1,2,3,4,5].forEach(() => { queue(a) });

// run 3 jobs at a time [1,2,3,4,5,6,7,8,9,10].forEach(() => { queue(a, 3) });

<a name="queue"></a>

## queue(callback, [limit]) ⇒ <code>promise</code>
**Kind**: global function  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| callback | <code>function</code> |  | callback function that returns a promise or any other types |
| [limit] | <code>number</code> | <code>1</code> | number of callback to run at the same time, by default one callback at a time |

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.