1var retry = require('../lib/retry'); 2 3function attemptAsyncOperation(someInput, cb) { 4 var opts = { 5 retries: 2, 6 factor: 2, 7 minTimeout: 1 * 1000, 8 maxTimeout: 2 * 1000, 9 randomize: true 10 }; 11 var operation = retry.operation(opts); 12 13 operation.attempt(function(currentAttempt) { 14 failingAsyncOperation(someInput, function(err, result) { 15 16 if (err && err.message === 'A fatal error') { 17 operation.stop(); 18 return cb(err); 19 } 20 21 if (operation.retry(err)) { 22 return; 23 } 24 25 cb(operation.mainError(), operation.errors(), result); 26 }); 27 }); 28} 29 30attemptAsyncOperation('test input', function(err, errors, result) { 31 console.warn('err:'); 32 console.log(err); 33 34 console.warn('result:'); 35 console.log(result); 36}); 37 38function failingAsyncOperation(input, cb) { 39 return setImmediate(cb.bind(null, new Error('A fatal error'))); 40} 41