Created
April 1, 2014 17:35
-
-
Save icodejs/9919036 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create batches of values and apply a function asynchronously over each batch. | |
* Start new batch when the previous batch's promise has completed. | |
* @param {array} arr - List of values to be batched. | |
* @param {int} batchSize - Batch size. | |
* @param {Function} fn - Function to be applied on each array value. | |
* @return {Promise} - An array of arrays containing promises. | |
*/ | |
var mapLimit = function(arr, batchSize, fn) { | |
var batches = arr.reduce(function (accumulator, currVal, index) { | |
var createNewBatch = index % batchSize === 0; | |
var currentBatch = accumulator[accumulator.length - 1]; | |
if (createNewBatch) | |
accumulator.push([currVal]); | |
else | |
currentBatch.push(currVal); | |
return accumulator; | |
}, []); | |
var currentPromise = Q(); | |
var promises = batches.map(function (batch) { | |
currentPromise = currentPromise.then(function () { | |
// Map over current batch and run desired function on it. | |
var batchPromises = batch.map(fn); | |
return Q.allSettled(batchPromises); | |
}); | |
return currentPromise; | |
}); | |
// Return an array of settled promises with their states | |
return Q.all(_.flatten(promises)).then(_.flatten); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment