array-async-queue

1.0.7 • Public • Published

Install

npm install array-async-queue

About

Asynchronously go through an array with concurrency and queue

Example

const arrayAsyncQueue = require("array-async-queue");
const arr = [];
const concurrency = 10;

for (let i = 0; i < 1000; i++) {
  arr.push(i);
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

arrayAsyncQueue(arr, concurrency, (val, stop) => {
  console.log(val);

  if(getRandomInt(0, 100) >= 99) {
    // stop the queue if necessary
    return stop();
  }

  return new Promise(resolve => setTimeout(resolve, getRandomInt(0, 50))
})

Description

Initially, the functions will be launched in parallel in the quantity specified in the variable concurrency. After completing one of them, the next one remaining in the array will be called. At the same time, the number of parallel called functions is always equal to concurrency.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.7
    1
    • latest

Version History

Package Sidebar

Install

npm i array-async-queue

Weekly Downloads

8

Version

1.0.7

License

MIT

Unpacked Size

6.35 kB

Total Files

6

Last publish

Collaborators

  • ortex