Froggy
Froggy

Reputation: 35

The async.retry function runs once

I can't find a proper explanation of how to use the retry function from the async librarydocs

var async = require('async');
    
function callFunc(data, time, name, callback) {
     console.log("#")
     callback({message: data, time, name}, null); //error
     // callback(null, {message: "ok"});               // ok
}


var func = callFunc.bind(null, "data", "time", "name", function (err, data) {
     console.log(data);
     return err;
})
async.retry({times: 3, interval: 1000}, func, function (err, results) {
     console.log('===================================');
     console.log('Async function');
})

Example of what I implement(but simpler). Can you tell me what I'm doing wrong? Killed the whole day for this.

P.S. The function should be called three times on error.

Upvotes: 1

Views: 259

Answers (2)

Froggy
Froggy

Reputation: 35

var async = require('async');

function callFunc(data, time,callback) {
     console.log(data, time)
     callback({message: "err"}, null); // error
     // callback(null, {message: "ok"}); // ok
}

async.retry({times: 3, interval: 1000}, function(callback, results) {
     callFunc("test", "data", callback)
},
function (err, results) {
     console.log('===================================');
     console.log('Async function');
})

Upvotes: 0

Paul Serre
Paul Serre

Reputation: 800

The async.retry function expects a function that has the signature function (callback, results), where callback is a callback function that you should call when the operation is complete, and results is an object that contains the results of the operation so far.

Upvotes: 2

Related Questions