2.0.5 • Published 5 years ago

@coolgk/queue v2.0.5

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

@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

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)
});

queue(callback, limit) ⇒ promise

Kind: global function

ParamTypeDefaultDescription
callbackfunctioncallback function that returns a promise or any other types
limitnumber1number of callback to run at the same time, by default one callback at a time
2.0.5

5 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.0

6 years ago