Home > Mobile >  The async.retry function runs once
The async.retry function runs once

Time:12-27

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.

CodePudding user response:

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.

CodePudding user response:

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');
})
  • Related