@@ -7,42 +7,42 @@ function _sleep(time) {
77
88const 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 */
2525async 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
0 commit comments