Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router): remove partial Promise from router.go
BREAKING CHANGE: The `router.go()` methods doesn't return anything (like in Vue 3) anymore. The existing implementation was wrong as it would resolve the promise for the following navigation if `router.go()` was called with something that wasn't possible e.g. `router.go(-20)` right after entering the application would not do anything. Even worse, the promise returned by that call would resolve **after the next navigation**. There is no proper native API to implement this promise-based api properly, but one can write a version that should work in most scenarios by setting up multiple hooks right before calling `router.go()`: ```js export function go(delta) { return new Promise((resolve, reject) => { function popStateListener() { clearTimeout(timeout) } window.addEventListener('popstate', popStateListener) function clearHooks() { removeAfterEach() removeOnError() window.removeEventListener('popstate', popStateListener) } // if the popstate event is not called, consider this a failure const timeout = setTimeout(() => { clearHooks() reject(new Error('Failed to use router.go()')) // It's unclear of what value would always work here }, 10) setImmediate const removeAfterEach = router.afterEach((_to, _from, failure) => { clearHooks() resolve(failure) }) const removeOnError = router.onError(err => { clearHooks() reject(err) }) router.go(delta) }) } ```
- Loading branch information