You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
888 B

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