use-async.ts 425 B

12345678910111213141516
  1. import { isReactive, isRef } from 'vue';
  2. function setLoading(loading, val) {
  3. if (loading != undefined && isRef(loading)) {
  4. loading.value = val;
  5. } else if (loading != undefined && isReactive(loading)) {
  6. loading.loading = val;
  7. }
  8. }
  9. export const useAsync = async (func: Promise<any>, loading: any): Promise<any> => {
  10. setLoading(loading, true);
  11. return await func.finally(() => setLoading(loading, false));
  12. };