Skip to content

ortexx/array-async-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Asynchronously go through an array with concurrency and queue

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published