Skip to content

Commit

Permalink
fix(useFetch): update execute throwing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 7, 2021
1 parent ac7295a commit 5bfac71
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/core/useFetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ interface UseFetchReturnBase<T> {

/**
* Manually call the fetch
* (default not throwing error)
*/
execute: () => Promise<any>
execute: (throwOnFailed?: boolean) => Promise<any>

/**
* Fires after the fetch request has finished
Expand Down Expand Up @@ -108,11 +109,9 @@ export interface BeforeFetchContext {
}

export interface AfterFetchContext<T = any> {

response: Response

data: T | null

}

export interface UseFetchOptions {
Expand Down Expand Up @@ -268,7 +267,7 @@ export function useFetch<T>(url: MaybeRef<string>, ...args: any[]): UseFetchRetu
isFinished.value = !isLoading
}

const execute = async() => {
const execute = async(throwOnFailed = false) => {
loading(true)
error.value = null
statusCode.value = null
Expand Down Expand Up @@ -341,7 +340,10 @@ export function useFetch<T>(url: MaybeRef<string>, ...args: any[]): UseFetchRetu
.catch((fetchError) => {
error.value = fetchError.message || fetchError.name
errorEvent.trigger(fetchError)
reject(fetchError)
if (throwOnFailed)
reject(fetchError)
else
resolve(undefined)
})
.finally(() => {
loading(false)
Expand Down

0 comments on commit 5bfac71

Please sign in to comment.