Created
August 25, 2016 09:22
-
-
Save ariesjia/1c718191cf91378601f44330a415d607 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
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