Skip to content

Commit

Permalink
feat(vue3-hooks): 内部request返回promise,使其在可以在外部调用时可以await
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed May 14, 2023
1 parent ac2e160 commit 8156289
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/vue3-hooks/src/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useRequest<
defaultData?: DF,
): ToRefs<State<REQ, DF>> & {
setInnerRequest<T extends (...args: unknown[]) => void>(cb: (req: T) => T): void;
} & Record<ALIAS, (...args: Parameters<REQ>) => void>;
} & Record<ALIAS, (...args: Parameters<REQ>) => Promise<void>>;
/**
* vue3 请求hooks @overload `数据驱动`
*
Expand Down Expand Up @@ -127,21 +127,21 @@ export function useRequest(requestFn: FN, options = {}, defaultData = null) {
..._loadingOptions,
};

const state = reactive({
const state = reactive<State<FN, typeof defaultData>>({
loading: loadingOptions.immediate,
data: defaultData,
error: null,
});

const refs = toRefs(state);

let request = (...args: unknown[]) => {
let request = (...args: unknown[]): Promise<void> => {
// computed变量不能JSON.stringfy
args = args.map((item) => (isRef(item) ? item.value : item));
state.loading = true;
state.error = null;
const start = Date.now();
requestFn(...args)
return requestFn(...args)
.then(
(res) => (state.data = res.data),
(err) => {
Expand Down

0 comments on commit 8156289

Please sign in to comment.