Almost sure I messed up my promises, but here it goes, before my head explodes
I'm trying to implement a retry mechanism for my UnprocessedItems after a batch operation using aws dynamodb. I have an array of batches that I want to write to the database.
var promiseList = []
batches.forEach(batch => {
promiseList.push(save_batches(batch.params));
}
Promise.all(promiseList)
.then(data => {
console.log(data);
});
And the functions that actually saves the data:
function save_batches(params){
return Promise.resolve(save(params, MAX_ATTEMPTS));
}
function save(params, retry){
return docClient.batchWrite(params).promise() //aws docClient object
.then(data => {
if("Unprocessed items found") && (retry > 0) {
params.RequestItems = data.UnprocessedItems;
sleep("for some time").then(() => {
return save(params, retry--); //recursive call
})
}
return data; //return if everything is ok or retry == 0 and there is nothing else to do
});
}
The result data after calling Promise.all should be a list of results:
data = [[r1], [r2], [r3], [r4]]
Everything is ok when the batchWrite doesn't have any UnprocessedItems. But when a retry is needed I keep getting null values precisely for those batches that had some UnprocessedItems in the first run: for example, if the batches 2 and 3 resulted in some UnprocessedItems the promise.all ends with data = [[r1], null, null, [r4]]
I guess I'm handling the promises calls wrongly when using recursivity but I couldn't figure it out yet.