diff --git a/src/withRetryAsync/demo.vue b/src/withRetryAsync/demo.vue index e498604..e67bad0 100644 --- a/src/withRetryAsync/demo.vue +++ b/src/withRetryAsync/demo.vue @@ -8,7 +8,7 @@ function getApi(keywords: string) { method: 'GET', mode: 'cors', }).then((res) => { - if (res.status === 200) { + if (res.status >= 200 && res.status < 300) { return { data: `result for ${keywords}`, }; @@ -18,7 +18,13 @@ function getApi(keywords: string) { }); } -const autoRetryGetApi = withRetryAsync(getApi); +const autoRetryGetApi = withRetryAsync(getApi, { + maxCount: 3, + retryInterval: 1000, + onRetry(i) { + console.log('第%d次尝试', i + 1); + }, +}); export default defineComponent({ setup() { diff --git a/src/withRetryAsync/index.ts b/src/withRetryAsync/index.ts index 0475240..a629f82 100644 --- a/src/withRetryAsync/index.ts +++ b/src/withRetryAsync/index.ts @@ -1,6 +1,6 @@ export default function withRetryAsync( fn: (this: T, ...p: P) => Promise, - { maxCount = 3, retryInterval = 500 } = {}, + { maxCount = 3, retryInterval = 500, onRetry = (i: number) => {} } = {}, ) { return function withRetryedAsync(this: T, ...args: P): Promise { return new Promise((resolve, reject) => { @@ -11,7 +11,7 @@ export default function withRetryAsync( execTask(); function execTask() { - console.log('第%d次尝试', retriedCount + 1); + onRetry(retriedCount); fn.call(that, ...args) .then((...r) => { resolve(...r);