asnyc await

vue async/await同步 案例

 

1.async/await场景

这是一个用同步的思维来解决异步问题的方案,当前端接口调用需要等到接口返回值以后渲染页面时。

2.名词解释

  >async

    async的用法,它作为一个关键字放到函数前面,用于表示函数是一个异步函数,因为async就是异步的意思, 异步函数也就意味着该函数的执行不会阻塞后面代码的执行,async 函数返回的是一个promise 对象。

 >await

   await的含义为等待。意思就是代码需要等待await后面的函数运行完并且有了返回结果之后,才继续执行下面的代码。这正是同步的效果

 >例子

  

复制代码
function fn() { 
 
    return new Promise((resolve, reject) => { 
 
        setTimeout(() => { 
 
            reject(hello vue.js'); 
 
        }, 1000); 
 
    }) 
 
} 
 
  
 
const foo = async () => { 
 
    try { 
 
        await fn(); 
 
    } catch (e) { 
 
        console.log(e);  // some error 
 
    } 
 
} 
复制代码

3.案例

  auth.vue

  需要注意:await必须放在async中 

复制代码
  import http from '../../utils/http'
  import api from '../../utils/api'

   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;
        
      }
}
复制代码

http.js

复制代码
'use strict'

import axios from 'axios'
import qs from 'qs'

axios.interceptors.request.use(config => {
  // loading
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  return response
}, error => {
  return Promise.resolve(error.response)
})

function checkStatus (response) {
  // loading
  // 如果http状态码正常,则直接返回数据
  if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {
    return response
    // 如果不需要除了data之外的数据,可以直接 return response.data
  }
  // 异常状态下,把错误信息返回去
  return {
    status: -404,
    msg: '网络异常'
  }
}

function checkCode (res) {
  // 如果code异常(这里已经包括网络错误,服务器错误,后端抛出的错误),可以弹出一个错误提示,告诉用户
  if (res.status === -404) {
    alert(res.msg)
  }
  if (res.data && (!res.data.success)) {
    alert(res.data.error_msg)
  }
  return res
}

export default {
  post (data,url) {
    return axios({
      method: 'post',
      url: url,
      data: qs.stringify(data),
      timeout: 10000,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
    }).then(
      (response) => {
        return checkStatus(response)
      }
    )
  },
  get (url, params) {
    return axios({
      method: 'get',
      baseURL: 'https://cnodejs.org/api/v1',
      url,
      params, // get 请求时带的参数
      timeout: 10000,
      headers: {
        'X-Requested-With': 'XMLHttpRequest'
      }
    }).then(
      (response) => {
        return checkStatus(response)
      }
    ).then(
      (res) => {
        return checkCode(res)
      }
    )
  }
}
复制代码

api.js

export default {
  getCode: 'http://127.0.0.1:8888/get_author'
}

 ================

一直以为es7里面的async和await可以用来简化串行异步代码,而没有想到还能并行。
说到底,这俩货不过是promise的语法糖,await的作用只是串行解析promise。
通常我们这样写:

function asyncAwaitFn(str) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(str)
        }, 1000);
    })
}
const parallel = async () => { //并行执行
    console.time('parallel')
    const parallelOne = await asyncAwaitFn('1');
    const parallelTwo = await asyncAwaitFn('2')
    console.log(parallelOne) //1
    console.log(parallelTwo) //2
    console.timeEnd('parallel') //2003.509033203125ms
}
parallel()

这是串行,显然最后的执行时间应该大于2000ms。

但如果换一种写法:

function asyncAwaitFn(str) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(str)
        }, 1000);
    })
}
const parallel = async () => { //并行执行
    console.time('parallel')
    const parallelOne = asyncAwaitFn('1');
    const parallelTwo = asyncAwaitFn('2')
    console.log(await parallelOne) //1
    console.log(await parallelTwo) //2
    console.timeEnd('parallel') //1001.87255859375ms
}
parallel()

最后执行时间只要1000ms,显然是并行了。

不过严谨来说,这依然是promise本身的并行罢了。

=====================

async表示函数里有异步操作,await表示紧跟在后面的表达式需要等待结果。

async函数返回一个 Promise 对象,可以使用then方法添加回调函数。当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句。

await 后面必须跟Promise对象,若非Promise,则不会拦截后面代码执行。当Promise对象resolve过后并且执行完then里面的代码,就执行下一步代码,不resolve则不会触发下一行代码执行,Promise对象处于pending状态

需注意:如果then()中需要异步操作,不会等then中的异步执行完过后再执行下一个then()的函数。原因就是,异步函数中,没有时间让你return值给下一个then()回调函数。解决方案是使用async函数包裹then中的回调函数,将其中的异步操作用new Promise((res, rej) => { 异步操作 })包裹,并用await等待异步操作完成。

举个例子:

const aa = _ => new Promise((res, rej) => { // 设置aa函数返回promise对象
    setTimeout(function() {
        console.log('1')
        res('2')
    }, 1000);
})
let bb = async function() {
  await aa().then((res) => { // await会等待aa()执行完,并且then代码也执行完,当然,then代码里面的异步操作不会执行。
    console.log(res)
    setTimeout(function(){  
        console.log('4') // then中的代码执行完直接进入下一个then,这个then其实return了undefined
        return 'sdf'
    }, 2000)
    // return '自定义插入'
  }).then(res => {
      console.log(res) // undefined
  }).catch(err => {
      console.log(err)
  })
  console.log('3')
}
console.log(bb()) // Promise {<pending>}
// console结果
// Promise { <pending> }
// 1
// 2
// undefined
// 3
// 4

目标:当A异步任务完成后,继续并发n个B异步任务,使用asnyc函数实现

const fs = require('fs')
const readdir = function (path) { // 读文件函数
    return new Promise((res, rej) => {
        fs.readdir(path, (err, files) => {
            if (err) res(err)
            res(files)
        })
    })
}
const stat = function (path) { // 确认是否是文件夹
    return new Promise((res, rej) => {
        fs.stat(path, (err, file) => {
            if (err) res(err)
            file.isDirectory() ? res('dir') : res('file') // 返回类型结果
        })
    })
}
const getdirs = async function (filepath) {
    let sendFiles = []
    const files = await readdir(filepath) // 读文件夹
    const promises = files.map(function (file) { // 利用map函数特性,返回值组成新的数组,这儿并没有用async函数,map内并不等待一个stat回来后再进行另一个stat,是同时进行的。
        return stat(filepath + '/' + file)
            .then(res => {
                if (res === 'dir') sendFiles.push(file)
            })
    })
    await Promise.all(promises) // 这儿是异步并发的关键,在这个位置等待所有promise对象resolve。
    // console.log(sendFiles)
    return sendFiles
}
const dir = getdirs('../wepy-redux-boilerplate/src/store')
console.log('>>>', dir) // async函数运行结果为Promise对象,使用then方法获取返回值
dir.then(v => console.log(v))

作者:吃个石头
原始链接:https://www.zhihu.com/question/41493194/answer/91299155
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


原文地址:https://www.cnblogs.com/fs0196/p/12500892.html