简体   繁体   中英

Using await in Vue interceptor

How do I use await inside of an interceptor? I have tried the below code but it does not print the value.

http.interceptors.push(async (req, next) => {
    final value = await someAsyncFunction();
    console.log(value);
})

I also tried this code, which works but I want to use await since I need to execute some code after it synchronously.

http.interceptors.push(async (req, next) => {
    someAsyncFunction().then(value => console.log(value));
    executeSomethingElse();
})

I think you can't use the anonymous function on the interceptor, so you should write it like these

http.interceptors.push(async function (req, next) {
    await someAsyncFunction()
})

This is because Vue cannot access the this property on the background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM