From b94eb3b22cee190c1e7989cbf12014695ef47943 Mon Sep 17 00:00:00 2001 From: popomore Date: Tue, 1 Oct 2019 02:26:39 +0800 Subject: [PATCH] refactor: better async function in useAPI --- src/useAPI/index.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/useAPI/index.ts b/src/useAPI/index.ts index 90e19c90b3..c081d8a4cf 100644 --- a/src/useAPI/index.ts +++ b/src/useAPI/index.ts @@ -19,16 +19,10 @@ export const configRequest = (method: () => any) => { const useAPI = (opt: IProps) => { const requestMethod = opt.method || globalMethod || fetch; return useAsync( - async () => - new Promise((resolve, reject) => { - requestMethod(opt.url, opt.options) - .then(async res => { - resolve(res.json && typeof res.json === 'function' ? res.json() : res); - }) - .catch(e => { - reject(e); - }); - }), + async () => { + const res = await requestMethod(opt.url, opt.options); + return res.json && typeof res.json === 'function' ? res.json() : res; + }, [JSON.stringify(opt)], { manual: opt.manual,