-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: 회원가입 시도 시 이미 있는 이메일일 경우 에러메세지 띄우는 부분 수정 중
- Loading branch information
Showing
3 changed files
with
81 additions
and
30 deletions.
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
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,36 +1,31 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { useState, useCallback } from 'react'; | ||
|
||
type AsyncFunction<TArgs extends any[], TResult> = ( | ||
...args: TArgs | ||
) => Promise<TResult>; | ||
type AsyncFunction<T, A extends any[]> = (...args: A) => Promise<T>; | ||
|
||
interface UseAsyncResult<TResult> { | ||
pending: boolean; | ||
error: Error | null; | ||
requestFunction: (...args: any[]) => Promise<TResult | undefined>; | ||
} | ||
|
||
export default function useAsync<TArgs extends any[], TResult>( | ||
asyncFunction: AsyncFunction<TArgs, TResult> | ||
): UseAsyncResult<TResult> { | ||
const [pending, setPending] = useState(false); | ||
const useAsync = <T, A extends any[]>(asyncFunction: AsyncFunction<T, A>) => { | ||
const [pending, setPending] = useState<boolean>(false); | ||
const [error, setError] = useState<Error | null>(null); | ||
const [result, setResult] = useState<T | null>(null); | ||
|
||
const requestFunction = useCallback( | ||
async (...args: TArgs): Promise<TResult | undefined> => { | ||
async (...args: A) => { | ||
setPending(true); | ||
setError(null); | ||
try { | ||
setError(null); | ||
setPending(true); | ||
return await asyncFunction(...args); | ||
} catch (error) { | ||
setError(error as Error); | ||
return; | ||
const response = await asyncFunction(...args); | ||
setResult(response); | ||
return response; | ||
} catch (err) { | ||
setError(err as Error); | ||
return null; | ||
} finally { | ||
setPending(false); | ||
} | ||
}, | ||
[asyncFunction] | ||
); | ||
|
||
return { pending, error, requestFunction }; | ||
} | ||
return { pending, error, result, requestFunction }; | ||
}; | ||
|
||
export default useAsync; |
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