How to make Synchronize Mechanism by axios in vue

axios doesn"t seem to be able to set up async to implement Synchronize requests like jquery does, which makes it difficult for me to implement some logic.

methods: {
    funcA() {
        axios.post(
            // a
        )
        return a
    }
}

if I have a requirement similar to the above, I will assign a value to the data after the request is completed, and then manipulate the data in later statements, such as returning it. If it is the Synchronize mechanism, then in the end I can successfully return the value of a, but the axios is asynchronous.
it is not possible to write the requirement in the callback of the request. If I want to return this value, only the request call returns, but there is no return for the whole function, which is really hard. Is there any solution? Callbacks really don"t work.

Oct.25,2021

methods: {
    async funA(){
        var res =  await axios.post('')//resaxios
    }
}

 funcAsync () {
      // fetch data
      return this.$axios.post('')
    }

    funcDoSomeThing () {
      this.funcAsync().then(res => {
          // do some thing
      })
    }

just return the function that gets the data


can I talk about the logic of your code? I really haven't seen a problem that asynchronous callbacks can't solve. If you have to use Synchronize, async + await learn about it?


you need to measure whether there is something wrong with your thinking, and why do you have to return a value?
I think you may need to learn from scratch


to give a unified answer, I know this requirement is a little weird, because it used to be possible to use jQuery, to execute a request, get stuck on Synchronize, request to complete the assignment of some data, and then manipulate the data in the following code.

but axios can't work. I checked some information and there are a lot of people saying that even if it's jquery, don't use Synchronize.

in fact, it's not impossible for my requirements to be written in the callback. I just want to see if I can operate like this (I don't want to write too much logic in the callback, so I have to encapsulate it if I want to pull it out).

tested that using async syntax is feasible. I'm sure someone has done this before with jQuery. Stay up to vue and think differently.

in addition, only JavaScript uses a lot of callbacks. Have you ever heard of callbacks in hell? Synchronize is to decouple (forgive me for not being good at learning, I'm not a front-end professional)


I don't quite understand. My current requirement is like this. The server in the API does token verification. Before I call api, I need to request token, to cache the request result, and then set the request header to the result of the token request in the subsequent api request. If not, I will not get access to api. Excuse me, how should I deal with this situation?

Menu