Skip to content

Instantly share code, notes, and snippets.

@ariesjia
Created August 25, 2016 09:22
Show Gist options
  • Save ariesjia/1c718191cf91378601f44330a415d607 to your computer and use it in GitHub Desktop.
Save ariesjia/1c718191cf91378601f44330a415d607 to your computer and use it in GitHub Desktop.
export const setAsyncRouteLeaveHook = (router, route, hook)=> {
let withinHook = false;
let finalResult = undefined;
let finalResultSet = false;
router.setRouteLeaveHook(route, nextLocation => {
withinHook = true
if (!finalResultSet) {
hook(nextLocation).then(result => {
finalResult = result
finalResultSet = true
if (!withinHook && nextLocation) {
router.push(nextLocation)
}
})
}
const result = finalResultSet ? finalResult : false
withinHook = false
finalResult = undefined
finalResultSet = false
return result
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment