1

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.

Alexander Patrikalakis
  • 5,054
  • 1
  • 30
  • 48
YOBB
  • 125
  • 1
  • 10
  • 1
    you should `return sleep("for some time");` - otherwise your only return is `return data` - therefore, the recursed calls to save are effectively discarded – Jaromanda X Feb 22 '17 at 10:36
  • what doesn't help is that the inner call to save has extra arguments, meaning that params will be batch, retry will be params ... and the whole thing will fail spectacularly – Jaromanda X Feb 22 '17 at 10:46
  • @JaromandaX recursive + cursed = recursed? :-D – Bergi Feb 22 '17 at 11:41
  • @Bergi - OK, yes, that'll do :p – Jaromanda X Feb 22 '17 at 11:44
  • @JaromandaX I tried with and without the return getting somehow the same results. However you are right, it should be a return there. I discovered the null values were coming for a different reason. After putting a catch, I noticed a `ProvisionedThroughputExceededException`. The extra parameters were just a mistake, I forgot to remove them when preparing the example. Your comment actually solved the problem. If you post it as an answer I can mark it as correct. – YOBB Feb 23 '17 at 00:46

0 Answers0