Skip to content

Commit

Permalink
fix: do not retry when fetch is aborted (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
amihhs authored Jul 29, 2022
1 parent 9fed5b4 commit b73fe67
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
const { fetch, Headers } = globalOptions

function onError (ctx: FetchContext): Promise<FetchResponse<any>> {
// Is Abort
// If it is an active abort, it will not retry automatically.
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
const isAbort = (ctx.error && ctx.error.name === 'AbortError') || false
// Retry
if (ctx.options.retry !== false) {
if (ctx.options.retry !== false && !isAbort) {
const retries = typeof ctx.options.retry === 'number'
? ctx.options.retry
: (isPayloadMethod(ctx.options.method) ? 0 : 1)
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ describe('ohmyfetch', () => {
const err = await $fetch('', { baseURL: getURL('404'), retry: 3 }).catch(err => err)
expect(err.request).to.equal(getURL('404'))
})

it('abort with retry', () => {
const controller = new AbortController()
async function abortHandle () {
controller.abort()
const res = await $fetch('', { baseURL: getURL('ok'), retry: 3, signal: controller.signal })
console.log('res', res)
}
expect(abortHandle()).rejects.toThrow(/aborted/)
})
})

0 comments on commit b73fe67

Please sign in to comment.