Skip to content

Commit 5ad45cb

Browse files
committed
Fix tests and improve the readme
1 parent dbdcc63 commit 5ad45cb

12 files changed

+298
-204
lines changed

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,41 @@ retry(retrier : Function, opts : Object) => Promise
3838
- The supplied function receives two parameters
3939
1. A `Function` you can invoke to abort the retrying (bail)
4040
2. A `Number` identifying the attempt. The absolute first attempt (before any retries) is `1`.
41-
- The `opts` are same as for `node-retry`. Read [its docs](https://github.com/tim-kos/node-retry)
41+
- The `opts`
4242
* `retries`: The maximum amount of times to retry the operation. Default is `10`.
4343
* `factor`: The exponential factor to use. Default is `2`.
4444
* `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`.
4545
* `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`.
4646
* `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `true`.
4747
* `onRetry`: an optional `Function` that is invoked after a new retry is performed. It's passed the `Error` that triggered it as a parameter.
48+
49+
All time values are in milliseconds.
50+
51+
### Retries Explained
52+
53+
The formula used to calculate the individual timeouts is:
54+
55+
```
56+
Math.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout)
57+
```
58+
59+
Have a look at [this article][article] for a better explanation of approach.
60+
61+
If you want to tune your `factor` / `times` settings to attempt the last retry
62+
after a certain amount of time, you can use wolfram alpha. For example in order
63+
to tune for `10` attempts in `5 minutes`, you can use this equation:
64+
65+
![screenshot](https://github.com/OlliV/async-retry-ng/raw/master/equation.gif)
66+
67+
Explaining the various values from left to right:
68+
69+
* `k = 0 ... 9`: The `retries` value (10)
70+
* `1000`: The `minTimeout` value in ms (1000)
71+
* `x^k`: No need to change this, `x` will be your resulting factor
72+
* `5 * 60 * 1000`: The desired total amount of time for retrying in ms (5 minutes)
73+
74+
To make this a little easier for you, use wolfram alpha to do the calculations:
75+
76+
<http://www.wolframalpha.com/input/?i=Sum%5B1000*x^k%2C+{k%2C+0%2C+9}%5D+%3D+5+*+60+*+1000>
77+
78+
[article]: http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html

__tests__/basic.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test("bail + return", async () => {
6868
expect(error.message).toBe("woot");
6969
});
7070

71-
test("bail error", async t => {
71+
test("bail error", async () => {
7272
let retries = 0;
7373

7474
try {
@@ -86,7 +86,7 @@ test("bail error", async t => {
8686
expect(err.message).toBe("Wont retry");
8787
}
8888

89-
t.deepEqual(retries, 1);
89+
expect(retries).toBe(1);
9090
});
9191

9292
test("with non-async functions", async () => {

__tests__/test-forever.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
const retry = require("../lib/retry");
22

3-
test("testForeverUsesFirstTimeout", done => {
4-
const operation = retry.operation({
5-
retries: 0,
6-
minTimeout: 100,
7-
maxTimeout: 100,
8-
forever: true
9-
});
3+
test("testForeverUsesFirstTimeout", () => {
4+
return new Promise(done => {
5+
const operation = retry.operation({
6+
retries: 0,
7+
minTimeout: 100,
8+
maxTimeout: 100,
9+
forever: true
10+
});
1011

11-
operation.attempt(function(numAttempt) {
12-
console.log(">numAttempt", numAttempt);
13-
var err = new Error("foo");
14-
if (numAttempt == 10) {
15-
operation.stop();
16-
done();
17-
}
12+
operation.attempt(function(numAttempt) {
13+
console.log(">numAttempt", numAttempt);
14+
const err = new Error("foo");
15+
if (numAttempt === 10) {
16+
operation.stop();
17+
done();
18+
}
1819

19-
if (operation.retry(err)) {
20-
return;
21-
}
20+
if (operation.retry(err)) {
21+
}
22+
});
2223
});
2324
});

0 commit comments

Comments
 (0)