Async.retry executes immediately before waiting for interval
Asked Answered
J

2

6
async.retry({times : 25,interval : 30000},myFunction.bind(functionData),function(err,results){
console.log("===================================")
console.log("Async function finished processing")
return;
})

myFunction is called immediately and that too 5 times which is default. also there is no waiting period between calls

Judge answered 12/9, 2015 at 9:57 Comment(1)
can you show the code for myFunction?Voucher
S
2

This is a version issue I think.

Older versions of async.retry can only be called with a number as the first agument (eg see the v1.2.0 docs)

It does not accept the opts object. So if you pass it in instead of a number for the first argument, it defaults to no interval and a retry count of 5.

I had this same issue using v0.9.0 of the library, updating to v1.4.2 fixed the issue.

Simulcast answered 13/10, 2015 at 17:37 Comment(0)
V
0

The retry is dependent on the callback within your function. If the first argument of the callback is not falsy, then it will retry based on your times and interval settings. For example:

var async = require('async');
var count = 0;
var functionData = { some: 'data' };
var myFunction = function(callback, results) {
  console.log(++count);
  process.nextTick(function() {
    if (count < 5) { // Fail 5 times
      return callback({ message: 'this failed' }, null);
    }
    callback(null, { message: 'this succeeded' });
  });
};

async.retry({times : 25, interval : 1000}, myFunction.bind(functionData), function(err, results) {
  console.log("===================================")
  console.log("Async function finished processing")
  return;
});

This outputs:

1
2
3
4
5
===================================
Async function finished processing

With a 1 second interval between each attempt

Voucher answered 12/9, 2015 at 10:40 Comment(4)
async.retry(opts,function(callback){ console.log("XXXX"); return callback(new Error("sda")); },function(err){ callback(err); });Judge
problem is async.retry executes 5 times even when i am using {times : 25, interval : 60000}Judge
apart from the undefined callback, the snippet you pasted in you're 1st comment works as expected for me. I.e. it retries 25 times.Voucher
try lowering the interval to test it. maybe your getting a timeout elsewhere after waiting for 5 minutes?Voucher

© 2022 - 2024 — McMap. All rights reserved.