async/await

作者: 姜治宇 | 来源:发表于2020-05-22 16:13 被阅读0次

async/await是非常好用的一个语法,需结合promise来使用。
比如有两个串行接口,第一个是获取authCode,第二个是获取accessToken,这个在微信中比较常见:

axios({ url:'/api/authCode',method:'get' });
axios({ url:'/api/accessToken',method:'get' });

要获取accessToken,则必先获取authCode,这样就形成了等待关系。
我们知道async和await是生成器的语法糖,生成器是函数的中断机制,我们要知道什么时候中断、中断后从哪里继续执行,因此我们可以将接口包装成promise。

import axios from 'axios'
export default {
    name: "test2",
    methods:{
        getAuthCode(){//需包装成promise
          
            return new Promise((resolve,reject)=>{
                axios({url:'/api/authCode',method:'get'}).then(res=>{
                    resolve(res) //中断返回
                }).catch(err=>{
                    reject(err)
                })
            })

        },
        getAccessToken(){//需包装成promise
          
            return new Promise((resolve,reject)=>{
                axios({url:'/api/accessToken',method:'get'}).then(res=>{
                    resolve(res) //中断返回
                }).catch(err=>{
                    reject(err)
                })
            })

        },
        async getList(){//获取用户列表信息
            let res1 = await this.getAuthCode()
            let res2 = await this.getAccessToken(res1.data.authCode)

            let token = res2.data.accessToken
            if(token) {
                axios({url:`/api/getUserList?accessToken=${token}`,method:'get'}).then((res)=>{
                    console.log(res)
                }).finally(()=>{

                })
            }
        }
    },
    mounted() {
        this.getList()//跟正常调用函数一样的
    }

}

如果是箭头函数,我们可以这样写:

this.$refs['weekForm'].validate((valid) => {
    if (valid) {
        this.$Modal.confirm({
            title: '温馨提示',
            content: '<p>确定要提交吗?</p>',
            onOk: async () => {
                let result1 = await this.getAuthCode()

                let result2 = await this.getAccessToken(result1.data.authCode)
                console.log('result1>>>', result1)
                console.log('result2>>>', result2)
                if (result1.data.code === 0 && result2.data.code === 0) {

                    this.$Message.success('成功')

                }


            },
            onCancel: () => {
                //this.nextStep = false
            }
        });
    }
})

相关文章

  • async和await

    浅谈Async/Await用 async/await 来处理异步 async和await async:声明一个异步...

  • ES8(一) —— async&await

    目录 async和普通函数的区别 await async/await处理多回调异步 async和await必须配合...

  • async

    async/await特点 async/await更加语义化,async是“异步”的简写,async functi...

  • ES6中的好东西

    1 Await/Async 前端的回调时代我没有赶上,我赶上的是await/async时代。await和async...

  • Vue接口调用方式(三)async/await用法

    async/await用法 1. async/await的基本用法 async/await是ES7引入的新语法,可...

  • nodejs async 使用简介

    async await node async 使用

  • JS 中的 async/await

    async/await 是什么? async/await 是 ES2017 中新增的异步解决方案; await 只...

  • ES2017 async 函数

    async 和 await 在干什么 每个名称都有意义async : 异步await: async wait简...

  • async/await

    async/await async/await 是Generator的语法糖。 Generator 通过将Gene...

  • 小程序-云开发

    async和await async:异步(无等待)await:等待(是为了同步) 一、await 关键字只在 as...

网友评论

    本文标题:async/await

    本文链接:https://www.haomeiwen.com/subject/icnqahtx.html