Last active
February 14, 2023 20:17
-
-
Save BARNZ/b778feeb9e257597db99b183819ecfd9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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