Created
December 11, 2019 01:26
-
-
Save coltborg/86af5b8ec480f9c2e868b4fe6e163c8b to your computer and use it in GitHub Desktop.
Vue 3 Essentials - Sharing state
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
import { ref } from "@vue/composition-api" | |
export default function usePromise(fn) { | |
const results = ref(null); | |
const loading = ref(false); | |
const error = ref(null); | |
const createPromise = async (..args) => { | |
loading.value = true; | |
error.value = null; | |
results.value = null; | |
try { | |
results.value = await fn(...args); | |
} catch (err) { | |
error.value = err; | |
} finally { | |
loading.value = false; | |
} | |
} | |
return { createPromise, error, loading, results }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment