[Vuejs]-My asynchronous javascript gets executed in the middle of the other function

2👍

this.auth().then(this.checkIfLoggedIn(result))

You have two problems.

First: this.checkIfLoggedIn(result) calls checkIfLoggedIn immediately. You need to pass a function to then.

this.auth().then(() => this.checkIfLoggedIn(result))

Second: With that change, you call checkIfLoggedIn when auth resolves.

So when does auth resolve? Well, it is defined with the async keyword, so it resolves when it returns (unless it returns a promise, in which case it adopts that promise instead).

So what does it return? It has no return statement, so it returns undefined when it gets to the end … which is immediately after the call to axios (since you aren’t awaiting that).

If you returned the return value of axios(...).etc then it wouldn’t resolve until that promise resolved.

(Aside: You’re using async, you should probably refactor to use await, try {} catch() {} instead of .then() and .catch()).

Leave a comment