Skip to content

Instantly share code, notes, and snippets.

@BARNZ
Last active February 14, 2023 20:17
Show Gist options
  • Save BARNZ/b778feeb9e257597db99b183819ecfd9 to your computer and use it in GitHub Desktop.
Save BARNZ/b778feeb9e257597db99b183819ecfd9 to your computer and use it in GitHub Desktop.
/**
* Execute the given function and set the given ref to true while it is busy executing
*/
export function trackState(stateRef, fn) {
stateRef.value = true
return Promise.resolve(fn())
.finally(() => stateRef.value = false)
}
// Example usage
// Before
async function loadImages(paging) {
isLoading.value = true
let res = await get(props.url, paging)
paging.total = res.total
isLoading.value = false
return res.data
}
// After
async function loadImages(paging) {
return trackState(isLoading, async () => {
let res = await get(props.url, paging)
paging.total = res.total
return res.data
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment