-1

When I wrote the following code:

axios.post('/user', { email: this.email })
.then(
    (response) => {
      if (!response.data) {
        this.$router.push({ path: '/register', params: { user: defaultUser } });
      } else {
        this.$router.push({ path: '/login', params: { user: response.data } });
      }
    }, 
    (error) => {
      console.log(error);
    }
);

The compiler shows error @typescript-eslint/no-floating-promises: Promises must be handled appropriately or explicitly marked as ignored with the void operator.

I also tried put a catch block after then block but it's still the same problem.

axios.post('/user', { email: this.email })
  .then((response) => {
    if (!response.data) {
      this.$router.push({ path: '/register', params: { user: defaultUser } });
    } else {
      this.$router.push({ path: '/login', params: { user: response.data } });
    }
  })
  .catch((error) => {
    console.log(error);
  });

I also figured out that the problem disappears when I comment out this.$router.push. So the problem might deal with redirecting route and resolving promise. Maybe it's better to rephrase the problem to 'how to redirect route without leaving unresolved promise'? I hope I make the question clear.

ckouder
  • 13
  • 3

1 Answers1

0

OK finally I figured it out. this.$router.push() returns a promise which I didn't handle well. The final solution works as the following:

axios.post('/user', { email: this.email })
  .then((response) => {
    if (!response.data) {
      void this.$router.push({ path: '/register', params: { user: defaultUser } });
    } else {
      void this.$router.push({ path: '/login', params: { user: response.data } });
    }
  })
  .catch((error) => {
    console.log(error);
  });

Since having void would trigger eslint no-void syntax error, and I don't care about the return value of this promise, I suppressed the warning by adding /* eslint-disable no-void */.

ckouder
  • 13
  • 3