Last active
June 29, 2022 14:31
-
-
Save sorenlouv/cad155d622045d1fe5f98ac09cd4f92b to your computer and use it in GitHub Desktop.
Execute promises sequentially in batches
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
var Q = require('q'); | |
function batchPromises(items, fn, options) { | |
var results = []; | |
var index = (options.batchSize - 1); | |
function getNextItem() { | |
index++; | |
if (items.length > index) { | |
var nextItem = items[index]; | |
return getCurrentItem(nextItem); | |
} | |
} | |
function getCurrentItem(item) { | |
console.log(item, 'Starting'); | |
return fn(item) | |
.then(function(result) { | |
console.log(item, 'Success'); | |
results.push(result); | |
return getNextItem(); | |
}) | |
.catch(function() { | |
console.log(item, 'Error'); | |
return options.retry ? getCurrentItem(item) : getNextItem(); | |
}); | |
}; | |
var promises = items.slice(0, options.batchSize).map(function(item) { | |
return getCurrentItem(item); | |
}); | |
return Q.all(promises).then(function() { | |
return results; | |
}); | |
} | |
function handler(item) { | |
return Q.delay(400).then(function() { | |
if (Math.random() > 0.5) { | |
throw new Error('A random error occured'); | |
} | |
return item; | |
}); | |
} | |
var options = { | |
batchSize: 3, | |
retry: true | |
} | |
batchPromises(['a', 'b', 'c', 'd', 'e', 'f'], handler, options).then(function(items) { | |
console.log('Succeeded', items); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment