@hypercolor/promise-queue
v0.0.8
Published
Used by the Hypercolor Digital team to support concurrent looping over an array. Supports a concurrency count to control how many promises may be resolved at once
Downloads
474
Readme
Hypercolor Promise Queue
Table of Contents
Introduction
This tool is used by the team at Hypercolor Digital to loop through a list of promises and execute them in a queue. This is useful for when you have a list of promises that you want to execute in a specific order, but you don't want to wait for the previous promise to resolve before executing the next one. Additionally, this tool allows you to set a maximum number of concurrent promises that can be executed at any given time.
Installation
- NPM
npm i @hypercolor/promise-queue
- Yarn
yarn add @hypercolor/promise-queue
Usage
Example:
import { PromiseQueue } from '@hypercolor/promise-queue';
const exampleFunction = async (array: any[]) => {
/*
Loop through the array and execute a promise for each item.
Accepts argument for maxConcurrent Promises to process.
*/
//Void Returning Example:
await new PromiseQueue(1).runall(array.map((item, index) => async () => {
//do something with the item
await doSomething(item);
console.log("Processed item " + index + 1 + ' of ' + array.length + ' items.);
}));
// Return Array of Processed Items:
const results = await new PromiseQueue(1).runall(array.map((item, index) => async () => {
//do something with the item
const result = doSomethingAndReturnResult(item);
console.log("Processed item " + index + 1 + ' of ' + array.length + ' items.);
return result;
}));
};
More Information
Toolchain
- TypeScript
- p-Queue