@vsr.common/batch-promises
v1.3.5
Published
A utility to batch promises
Downloads
2
Readme
Batch Promises
A simple solution to batch promises
Why?
While working on my project, I encountered a significant roadblock: the lack of a robust solution for batching promises effectively. This inspired me to create batch-promises, a solution that not only fills this gap but also simplifies asynchronous code management.
By adopting batch-promises, you gain the ability to:
- Optimize Performance: Execute promises strategically, reducing unnecessary resource usage and potential bottlenecks.
- Improve Code Readability: Benefit from clear async/await integration, making your asynchronous logic easier to understand and maintain.
- Error Handling Made Easy: Employ the structured [successResults, errorResults] return value to gracefully handle both successful and failed promises.
Key Features:
- Enhanced Control and Efficiency: Execute promises in well-defined batches, optimizing resource utilization and preventing application overload.
- Simplified Async/Await Syntax: Enjoy intuitive async/await support for cleaner and more readable asynchronous operations.
- Clear Error Handling: The batchPromises function returns a two-element array: [successResults, errorResults], providing a structured approach to handling both successful and rejected promises.
Installation
npm i @vsr.common/batch-promises
🚀 Usage
Async/await way
import { batchPromises } from '@vsr.common/batch-promises';
const arr = [1, 2, 3, 4, 5];
const batchSize = 2;
const [success, error] = await batchPromises(batchSize, arr, (ele) =>
Promise.resolve(ele)
);
Promise way
const { batchPromises } = require('@vsr.common/batch-promises');
const arr = [1, 2, 3, 4, 5];
const batchSize = 2;
batchPromises(batchSize, arr, (ele) =>
Promise.resolve(ele)
).then(([success, error]) => {
// do your thing!
});