vue中async await请求处理

Promise.all() 用法示例:

const wait = ms => new Promise((resolve, reject) => {
    setTimeout(() => {
        console.log(`wait ${ms}ms`)
        resolve()
    }, ms)
})
const PA = Promise.all([wait(3000), wait(1000), wait(2000)])
// 依次打印:wait 1000ms wait 2000ms wait 3000ms

async-await 同时触发多个异步操作示例

const wait = ms => new Promise((resolve, reject) => {
    setTimeout(() => {
        console.log(`wait ${ms}ms`)
        resolve()
    }, ms)
})
;(async () => {
    await Promise.all([wait(3000), wait(1000), wait(2000)])
    // 依次打印:wait 1000ms wait 2000ms wait 3000ms
})()

问题:接口B需要接口A返回的数据,如果同时请求,控制不了返回速度,会出现B快于A,

实现:

async表示函数里有异步操作,

await表示紧跟在后面的表达式需要等待结果。

 

 

   methods: {
      fetchData: async function () {
        var that = this
        var code = Store.fetchYqm();
        let params = {
          inviteCode: code
        }
        const response = await http.post(params,api.getCode)
         var resJson = response.data;
        
      }
}

 

你可能感兴趣的:(vue)