Skip to content

Commit

Permalink
refactor: better async function in useAPI (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and ttys026 committed Oct 7, 2019
1 parent 7eeca08 commit d5d2709
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/useAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ export const configRequest = (method: () => any) => {
const useAPI = <T = any>(opt: IProps<T>) => {
const requestMethod = opt.method || globalMethod || fetch;
return useAsync<T>(
async () =>
new Promise<T>((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,
Expand Down

1 comment on commit d5d2709

@vercel
Copy link

@vercel vercel bot commented on d5d2709 Oct 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.