Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router.push & router.replace to return promise if callbacks are not provided #1769

Closed
charles-mathieus-jomedia opened this issue Sep 27, 2017 · 6 comments

Comments

@charles-mathieus-jomedia

What problem does this feature solve?

It would help preventing callback hell... Would be more consistent with vue js api...

What does the proposed API look like?

router.push('/new/url').then(() => doSomeCoolStuff())

@belmarca
Copy link

This seems to work fine if for example your component is expecting an event.

methods: {
  asyncRouterPush (route) {
    return new Promise(resolve => {
      resolve(this.$router.push({ path: route }))
    })
  },
  doSomething (myVar) {
    this.asyncRouterPush(myVar)
      .then(() => {
        this.$root.$emit('someEvent')
      })
  }
}

@vuejs vuejs deleted a comment from buzhou9 Mar 26, 2019
@adamreisnz
Copy link

adamreisnz commented Apr 16, 2019

Any progress on this? Seems a bit backwards in 2019 that we still have to use a callback API, and can't simply await router.push().

@adamreisnz
Copy link

Here's a simple helper function in the mean time...

/**
 * Async push method
 */
router.pushAsync = function(route) {
  return new Promise((resolve, reject) => {
    router.push(route, resolve, reject);
  });
};

@Mighty683
Copy link
Contributor

I will try implement this issue.

@Mighty683
Copy link
Contributor

Mighty683 commented Jul 30, 2019

The problem is UnhandledPromiseRejectionWarning if we dont implement catch for previous implementations.

router.push should throw error on route abort? Or should resolve with undefined on route abort?

https://github.com/vuejs/vue/blob/dev/src/core/util/next-tick.js should I implement this similar to nextTick?

@posva
Copy link
Member

posva commented Jul 30, 2019

Yes, it's similar to nextTick. It must work when Promises are not supported. The behavior only changes when no callback is passed and there is support for Promises

@posva posva closed this as completed in d907a13 Aug 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants