Multithread Synchronization Loop Library with Promise Support
npm install for-more> A powerful and flexible multithread synchronization loop library with Promise support, TypeScript integration, and cancelable execution.
- Features
- Installation
- Import
- Usage
- Basic Usage
- Async/Await Support
- Concurrency Control
- Error Handling
- Callback Support
- Cancel Execution
- Array-like Objects
- TypeScript Usage
- API Reference
- forMore(array, options, handler, callback)
- array.forMore(options, handler, callback)
- ForMoreOptions
- CancellablePromise
- Module Formats
- Browser Support
- Performance
- Contributing
- License
- 🔄 Multithread Synchronization Loop
- 🤝 Promise Support with async/await compatibility
- 📝 TypeScript Ready with full type definitions
- 🔧 Multiple Module Formats: CommonJS, ESM, UMD
- ⚡ Concurrency Control: Configure parallel execution with lines or concurrency
- 🛑 Cancelable Execution: Abort ongoing operations at any time
- 🎯 Flexible Error Handling: Custom error handlers and abort options
- 📦 Lightweight: Small bundle size (~2KB gzipped)
- 🚀 High Performance: Optimized for parallel execution
- 🔗 Array Prototype Extension: Use as array.forMore() method
- 🔢 Array-like Objects Support: Works with any array-like structure
``bash`
npm install for-more --saveor
yarn add for-moreor
pnpm add for-more
`js`
const forMore = require('for-more');
require('for-more'); // Extends Array.prototype
`js`
import forMore from 'for-more';
import 'for-more'; // Extends Array.prototype
`html`
`js
// Using as a function
forMore([1, 2, 3], 2, function(item, index, array) {
return item * 2;
}).then(results => {
console.log(results); // [2, 4, 6]
});
// Using as an Array method
[1, 2, 3].forMore(2, function(item, index, array) {
return item * 2;
}).then(results => {
console.log(results); // [2, 4, 6]
});
// Simplified syntax (default concurrency = 1)
[1, 2, 3].forMore(function(item) {
return item * 2;
}).then(results => {
console.log(results); // [2, 4, 6]
});
`
`jshttps://api.example.com/data/${item}
// Using async/await with forMore
async function processData() {
const results = await [1, 2, 3].forMore(2, async function(item, index, array) {
const response = await fetch();
return response.json();
});
console.log(results); // Array of fetched data
}
processData();
// Mixed sync and async operations
[1, 2, 3].forMore(2, function(item) {
if (item % 2 === 0) {
// Async operation for even numbers
return new Promise(resolve => {
setTimeout(() => resolve(item * 2), 100);
});
}
// Sync operation for odd numbers
return item * 2;
}).then(results => {
console.log(results); // [2, 4, 6]
});
`
`js
// Using lines option
[1, 2, 3, 4, 5].forMore({
lines: 3 // Run 3 parallel operations at a time
}, async function(item) {
await new Promise(resolve => setTimeout(resolve, 50));
return item;
}).then(results => {
console.log(results); // [1, 2, 3, 4, 5]
});
// Using concurrency option (alias for lines)
[1, 2, 3, 4, 5].forMore({
concurrency: 3 // Same as lines: 3
}, async function(item) {
await new Promise(resolve => setTimeout(resolve, 50));
return item;
}).then(results => {
console.log(results); // [1, 2, 3, 4, 5]
});
`
`js
// Default behavior: Continue on error
[1, 2, 3].forMore(2, function(item) {
if (item === 2) {
throw new Error('Test error');
}
return item * 2;
}).then(results => {
console.log(results); // [2, Error, 6]
});
// Abort on error
[1, 2, 3].forMore({
lines: 2,
abort: true // Stop execution on first error
}, function(item) {
if (item === 2) {
throw new Error('Test error');
}
return item * 2;
}).catch(error => {
console.error(error); // Test error
});
// Custom error handler
[1, 2, 3].forMore({
lines: 2,
onError: function(error, index, item) {
console.error(Error at index ${index}: ${error.message});`
return false; // Return true to abort, false to continue
}
}, function(item) {
if (item === 2) {
throw new Error('Test error');
}
return item * 2;
}).then(results => {
console.log(results); // [2, Error, 6]
});
`js
// Using callback instead of Promise
[1, 2, 3].forMore(2, function(item) {
return item * 2;
}, function(results, originalArray) {
console.log(results); // [2, 4, 6]
console.log(originalArray); // [1, 2, 3]
});
// With both Promise and callback (Promise still resolves)
[1, 2, 3].forMore(2, function(item) {
return item * 2;
}, function(results) {
console.log('Callback called:', results);
}).then(results => {
console.log('Promise resolved:', results);
});
`
`js
// Create a cancellable promise
const promise = [1, 2, 3, 4, 5].forMore(2, async function(item) {
await new Promise(resolve => setTimeout(resolve, 100));
return item;
});
// Cancel after 150ms
setTimeout(() => {
console.log('Cancelling execution...');
promise.cancel();
}, 150);
promise
.then(results => {
console.log('Completed:', results);
})
.catch(error => {
console.error('Error:', error);
});
`
`js
// Using with NodeList
const elements = document.querySelectorAll('div');
forMore(elements, 2, function(element, index) {
return element.textContent;
}).then(textContents => {
console.log(textContents);
});
// Using with custom array-like object
const arrayLike = { 0: 1, 1: 2, 2: 3, length: 3 };
forMore(arrayLike, 2, function(item) {
return item * 2;
}).then(results => {
console.log(results); // [2, 4, 6]
});
`
`typescript
import forMore from 'for-more';
import 'for-more'; // Extends Array.prototype
interface User {
id: number;
name: string;
}
const users: User[] = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }];
// Basic TypeScript usage
users.forMore
return user.id * 2;
}).then((results: number[]) => {
console.log(results); // [2, 4, 6]
});
// Async TypeScript usage
users.forMore
const response = await fetch(https://api.example.com/users/${user.id});
const data = await response.json();
return data.name;
}).then((results: string[]) => {
console.log(results); // Array of user names from API
});
// Using with options
users.forMore
lines: 2,
abort: true
}, (user: User) => {
return user.id * 2;
}).then((results: number[]) => {
console.log(results); // [2, 4, 6]
});
`
#### Parameters
- array: Array | ArrayLike - The array or array-like object to iterate overnumber | ForMoreOptions
- options: - Configuration options or number of parallel linesForMoreCallback
- handler: - The function to execute for each itemT
- item: - The current item being processednumber
- index: - The index of the current itemT[]
- array: - The original arrayU | Promise
- returns: - The result of processing the itemFunction
- callback: (optional) - Callback function when all items are processedU[]
- results: - The results of all processed itemsT[]
- originalArray: - The original array
#### Returns
- CancellablePromise - A promise that resolves with the results of all processed items
Same as forMore(array, options, handler, callback), but as a method on the Array prototype.
`typescript`
type ForMoreOptions = {
/* Number of parallel execution lines (default: 1) /
lines?: number;
/* Alias for lines, number of concurrent operations (default: 1) /
concurrency?: number;
/* Abort on error (default: false) /
abort?: boolean;
/* Custom Promise library (default: global Promise) /
promiseLibrary?: PromiseConstructor;
/* Callback function when all items are processed /
callback?: (results: any[], originalArray: any[]) => void;
/* Custom error handler, return true to abort /
onError?: (error: any, index: number, item: any) => boolean;
};
`typescript`
type CancellablePromise
/* Cancel ongoing execution /
cancel: () => void;
};
The library is available in multiple formats:
| Format | File | Entry Point |
|--------|------|-------------|
| CommonJS | dist/index.cjs.js | main |dist/index.esm.js
| ES Module | | module |dist/index.umd.js
| UMD | | unpkg |dist/index.umd.min.js
| UMD (minified) | | - |dist/index.d.ts
| TypeScript Definitions | | types |
| Chrome | Firefox | Safari | Edge | IE |
|--------|---------|--------|------|----|
| ✅ 60+ | ✅ 55+ | ✅ 11+ | ✅ 79+ | ✅ 10+ |
- Bundle Size: ~2KB gzipped
- Execution Speed: Optimized for parallel execution
- Memory Usage: Efficient memory management
- Scalability: Handles large arrays with ease
Contributions are welcome! Please feel free to submit a Pull Request.
`bashInstall dependencies
npm install
MIT
- Author: LinQuan
- GitHub: https://github.com/mlinquan/for-more
- NPM: https://www.npmjs.com/package/for-more
---
for-more - Your reliable multithread synchronization loop library! 🚀