-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# Todo | ||
|
||
- [ ] retryAsync | ||
- [ ] ~~cacheAsync~~ see [memoizee](https://github.com/medikoo/memoizee#memoizing-asynchronous-functions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<script lang="tsx"> | ||
import { defineComponent } from 'vue'; | ||
import { withRetryAsync } from '..'; | ||
function getApi(keywords: string) { | ||
console.log('fetching', keywords); | ||
return fetch(`https://httpbin.org/status/200,500,400?keywords=${keywords}`, { | ||
method: 'GET', | ||
mode: 'cors', | ||
}).then((res) => { | ||
if (res.status === 200) { | ||
return { | ||
data: `result for ${keywords}`, | ||
}; | ||
} else { | ||
throw new Error(res.statusText); | ||
} | ||
}); | ||
} | ||
const autoRetryGetApi = withRetryAsync(getApi); | ||
export default defineComponent({ | ||
setup() { | ||
return () => ( | ||
<fieldset> | ||
<legend>查询场景</legend> | ||
<button | ||
onClick={async () => { | ||
const rez = await autoRetryGetApi('abc'); | ||
console.log('fetched', rez); | ||
}} | ||
> | ||
Get something same(useSamePromise) | ||
</button> | ||
</fieldset> | ||
); | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export default function withRetryAsync<T, P extends any[], R>( | ||
fn: (this: T, ...p: P) => Promise<R>, | ||
{ maxCount = 3, retryInterval = 500 } = {}, | ||
) { | ||
return function withRetryedAsync(this: T, ...args: P): Promise<R> { | ||
return new Promise((resolve, reject) => { | ||
let retriedCount = 0; | ||
// let timer: ReturnType<typeof setTimeout>; | ||
const that = this; | ||
|
||
execTask(); | ||
|
||
function execTask() { | ||
console.log('第%d次尝试', retriedCount + 1); | ||
fn.call(that, ...args) | ||
.then((...r) => { | ||
resolve(...r); | ||
}) | ||
.catch((...e) => { | ||
// console.log('failed'); | ||
if (++retriedCount >= maxCount) { | ||
reject(...e); | ||
} else { | ||
setTimeout(execTask, retryInterval); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
## Intro | ||
|
||
自动重试。 | ||
|
||
## Demo | ||
|
||
> demo 以网络请求为例,请打开 Devtool 查看效果 | ||
<script setup> | ||
import Demo from './demo.vue' | ||
</script> | ||
|
||
<Demo /> | ||
<details> | ||
<summary>查看代码</summary> | ||
|
||
<<< src/withRetryAsync/demo.vue{13,29-34} | ||
|
||
</details> | ||
|
||
|
||
## Types | ||
|
||
```ts | ||
export default function withRetryAsync<T, P extends any[], R>( | ||
fn: (this: T, ...p: P) => Promise<R>, | ||
{ | ||
maxCount, | ||
retryInterval, | ||
}?: { | ||
maxCount?: number | undefined; | ||
retryInterval?: number | undefined; | ||
}, | ||
): (this: T, ...args: P) => Promise<R>; | ||
``` |