Skip to content

Commit 74b7074

Browse files
committed
feat: 修改参数名
1 parent 64228a6 commit 74b7074

2 files changed

Lines changed: 38 additions & 38 deletions

File tree

src/retry.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@ function _sleep(time) {
77

88
const MSG = {
99
ARG_VALIDATOR__FN: '[Retry] argument `validator` is not a function',
10-
ARG_RETRIES__INT: '[Retry] argument `retries` must be an integer',
11-
ARG_RETRIES__POS: '[Retry] argument `retries` must be greater than 0',
12-
ARG_TIMEOUT__NUM: '[Retry] argument `timeout` must be a number',
13-
ARG_TIMEOUT__POS: '[Retry] argument `timeout` must be greater than 0',
10+
ARG_ATTEMPTS__INT: '[Retry] argument `attempts` must be an integer',
11+
ARG_ATTEMPTS__POS: '[Retry] argument `attempts` must be greater than 0',
12+
ARG_INTERVAL__NUM: '[Retry] argument `interval` must be a number',
13+
ARG_INTERVAL__POS: '[Retry] argument `interval` must be greater than 0',
1414
}
1515

1616
/**
1717
* 一个简单的重试方法
1818
* @param {function} validator 函数或异步函数,检查器,判断是否满足检查条件
1919
* @param {Object} options 重试参数
20-
* - options.retries {number} 最大重试次数,默认5次
21-
* - options.timeout {number} 重试间隔(毫秒),默认10秒
20+
* - options.attempts {number} 最大重试次数,默认5次
21+
* - options.interval {number} 重试间隔(毫秒),默认10秒
2222
* - options.name {string} 为当前重试任务起个名字;如未提供,则会自动生成一个唯一 ID
2323
* - options.debug {boolean} 是否调试模式。调试模式会输出更多日志
2424
*/
2525
async function retry(validator, {
26-
retries = 5,
27-
timeout = 10000,
26+
attempts = 5,
27+
interval = 10000,
2828
name = '',
2929
debug = false,
3030
} = {}) {
3131

3232
if (typeof validator !== 'function') {
3333
throw Error(MSG.ARG_VALIDATOR__FN)
3434
}
35-
if (!Number.isInteger(retries)) {
36-
throw Error(MSG.ARG_RETRIES__INT)
35+
if (!Number.isInteger(attempts)) {
36+
throw Error(MSG.ARG_ATTEMPTS__INT)
3737
}
38-
if (retries < 0) {
39-
throw Error(MSG.ARG_RETRIES__POS)
38+
if (attempts < 0) {
39+
throw Error(MSG.ARG_ATTEMPTS__POS)
4040
}
41-
if (typeof timeout !== 'number') {
42-
throw Error(MSG.ARG_TIMEOUT__NUM)
41+
if (typeof interval !== 'number') {
42+
throw Error(MSG.ARG_INTERVAL__NUM)
4343
}
44-
if (timeout < 0) {
45-
throw Error(MSG.ARG_TIMEOUT__POS)
44+
if (interval < 0) {
45+
throw Error(MSG.ARG_INTERVAL__POS)
4646
}
4747

4848
// debug
@@ -54,12 +54,12 @@ async function retry(validator, {
5454
do {
5555
result = await validator()
5656
times++
57-
await _sleep(timeout)
57+
await _sleep(interval)
5858

5959
// debug
6060
if (debug) console.log(`[Retry] trying "${name}" #${times} and get result:`, result)
6161

62-
} while (times <= retries && !result)
62+
} while (times <= attempts && !result)
6363

6464
if (result) {
6565
// debug

test/retry.test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@ describe('retry()', () => {
88
expect.assertions(1)
99
await expect(retry()).rejects.toThrow(MSG.ARG_VALIDATOR__FN)
1010
})
11-
test('quit if `retries` is not an integer', async () => {
11+
test('quit if `attempts` is not an integer', async () => {
1212
expect.assertions(1)
1313
await expect(retry(function () {}, {
14-
retries: 1.1,
15-
timeout: 0,
16-
})).rejects.toThrow(MSG.ARG_RETRIES__INT)
14+
attempts: 1.1,
15+
interval: 0,
16+
})).rejects.toThrow(MSG.ARG_ATTEMPTS__INT)
1717
})
18-
test('quit if `retries` < 0', async () => {
18+
test('quit if `attempts` < 0', async () => {
1919
expect.assertions(1)
2020
await expect(retry(function () {}, {
21-
retries: -1,
22-
timeout: 0,
23-
})).rejects.toThrow(MSG.ARG_RETRIES__POS)
21+
attempts: -1,
22+
interval: 0,
23+
})).rejects.toThrow(MSG.ARG_ATTEMPTS__POS)
2424
})
25-
test('quit if `timeout` is not a number', async () => {
25+
test('quit if `interval` is not a number', async () => {
2626
expect.assertions(1)
2727
await expect(retry(function () {}, {
28-
retries: 1,
29-
timeout: 'asd',
30-
})).rejects.toThrow(MSG.ARG_TIMEOUT__NUM)
28+
attempts: 1,
29+
interval: 'asd',
30+
})).rejects.toThrow(MSG.ARG_INTERVAL__NUM)
3131
})
32-
test('quit if `timeout` < 0', async () => {
32+
test('quit if `interval` < 0', async () => {
3333
expect.assertions(1)
3434
await expect(retry(function () {}, {
35-
retries: 1,
36-
timeout: -123,
37-
})).rejects.toThrow(MSG.ARG_TIMEOUT__POS)
35+
attempts: 1,
36+
interval: -123,
37+
})).rejects.toThrow(MSG.ARG_INTERVAL__POS)
3838
})
3939
})
4040

@@ -44,8 +44,8 @@ describe('retry()', () => {
4444
await expect(retry(function () {
4545
return true
4646
}, {
47-
retries: 1,
48-
timeout: 0,
47+
attempts: 1,
48+
interval: 0,
4949
debug: true,
5050
})).resolves.toBeTruthy()
5151
})
@@ -54,8 +54,8 @@ describe('retry()', () => {
5454
await expect(retry(function () {
5555
return false
5656
}, {
57-
retries: 1,
58-
timeout: 0,
57+
attempts: 1,
58+
interval: 0,
5959
debug: true,
6060
})).rejects.toThrow()
6161
})

0 commit comments

Comments
 (0)