11cb0ef41Sopenharmony_civar retry = require('../lib/retry'); 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_cifunction attemptAsyncOperation(someInput, cb) { 41cb0ef41Sopenharmony_ci var opts = { 51cb0ef41Sopenharmony_ci retries: 2, 61cb0ef41Sopenharmony_ci factor: 2, 71cb0ef41Sopenharmony_ci minTimeout: 1 * 1000, 81cb0ef41Sopenharmony_ci maxTimeout: 2 * 1000, 91cb0ef41Sopenharmony_ci randomize: true 101cb0ef41Sopenharmony_ci }; 111cb0ef41Sopenharmony_ci var operation = retry.operation(opts); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci operation.attempt(function(currentAttempt) { 141cb0ef41Sopenharmony_ci failingAsyncOperation(someInput, function(err, result) { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci if (err && err.message === 'A fatal error') { 171cb0ef41Sopenharmony_ci operation.stop(); 181cb0ef41Sopenharmony_ci return cb(err); 191cb0ef41Sopenharmony_ci } 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci if (operation.retry(err)) { 221cb0ef41Sopenharmony_ci return; 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci cb(operation.mainError(), operation.errors(), result); 261cb0ef41Sopenharmony_ci }); 271cb0ef41Sopenharmony_ci }); 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciattemptAsyncOperation('test input', function(err, errors, result) { 311cb0ef41Sopenharmony_ci console.warn('err:'); 321cb0ef41Sopenharmony_ci console.log(err); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci console.warn('result:'); 351cb0ef41Sopenharmony_ci console.log(result); 361cb0ef41Sopenharmony_ci}); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_cifunction failingAsyncOperation(input, cb) { 391cb0ef41Sopenharmony_ci return setImmediate(cb.bind(null, new Error('A fatal error'))); 401cb0ef41Sopenharmony_ci} 41