Last change
on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
888 bytes
|
Line | |
---|
1 | var retry = require('../lib/retry');
|
---|
2 |
|
---|
3 | function 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 |
|
---|
30 | attemptAsyncOperation('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 |
|
---|
38 | function failingAsyncOperation(input, cb) {
|
---|
39 | return setImmediate(cb.bind(null, new Error('A fatal error')));
|
---|
40 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.