MCPcopy
hub / github.com/vercel/async-retry / retry

Function retry

lib/index.js:4–59  ·  view source on GitHub ↗
(fn, opts)

Source from the content-addressed store, hash-verified

2var retrier = require('retry');
3
4function retry(fn, opts) {
5 function run(resolve, reject) {
6 var options = opts || {};
7 var op;
8
9 // Default `randomize` to true
10 if (!('randomize' in options)) {
11 options.randomize = true;
12 }
13
14 op = retrier.operation(options);
15
16 // We allow the user to abort retrying
17 // this makes sense in the cases where
18 // knowledge is obtained that retrying
19 // would be futile (e.g.: auth errors)
20
21 function bail(err) {
22 reject(err || new Error('Aborted'));
23 }
24
25 function onError(err, num) {
26 if (err.bail) {
27 bail(err);
28 return;
29 }
30
31 if (!op.retry(err)) {
32 reject(op.mainError());
33 } else if (options.onRetry) {
34 options.onRetry(err, num);
35 }
36 }
37
38 function runAttempt(num) {
39 var val;
40
41 try {
42 val = fn(bail, num);
43 } catch (err) {
44 onError(err, num);
45 return;
46 }
47
48 Promise.resolve(val)
49 .then(resolve)
50 .catch(function catchIt(err) {
51 onError(err, num);
52 });
53 }
54
55 op.attempt(runAttempt);
56 }
57
58 return new Promise(run);
59}
60
61module.exports = retry;

Callers 2

index.jsFile · 0.85
getRandomTitleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…