Execute an array of promises in groups of predefined elements instead of all at once. If the group is 1, the execution will be synchronous.
With npm do:
npm install batch-promise
var batch = require( 'batch-promise' );
batch(promiseFunc, size)
.then(...)
- promiseFunc: the promise function. It will be executed with new Promise( promiseFunc ), so resolve & reject must be used to reach next promise group.
- size (optional): size of concurrent threads. If none, 1 will be applied.
- return : Returns a promise. It will be resolved when all promises will.
var batch = require( 'batch-promise' );
var promises = [];
// My promises will print a number at random delay -------
// Build your own promise array ;)
var foo = [0,1,2,3,4,5,6,7,8,9];
foo.forEach( function( item ){
promises.push( function( resolve, reject ){
setTimeout( function(){
console.log( '*' , item );
resolve( item );
} , Math.floor(Math.random() * 100) + 1 );
} );
} );
//--------------------------------------------------------
console.log( 'Print sequencially.. the same as: batch( promises )' );
batch( promises , 1 )
.then( function( results ){
console.log( "Results" , results );
} );
Date | Version | Comments |
---|---|---|
2016-05-31 | v1.0.0 | Initial version |
Submitted by Juan José Castaño