Fork me on github

异步

export async function fun1 () {
  res = await test.post(link, obj)
  res.data.data = JSON.parse(decrypt(res.data.data, password))
  return res
}

说明:await 让js进行等待 直到promise 执行并返回结果

import { fun1 } from '../../util/...js'
fun1().then(data => {
    Console.log('')
 }).catch(err => {
    Console.log('')
 })

说明:async代表异步的,用来表示一个异步的函数,返回一个promise----->可以使用then方法添加回调,catch抛出异常

 async function foo({ username, password }) {
  const { userId } = await ajax('login', { username, password })
  const { token } = await ajax('getToken', { userId })
  const res = await ajax('getOtherInfo', { token })
  // do something ...
}

ajax('login', { username, password })
    .then(({ userId }) => ajax('getToken', { userId }))
    .then(({ token }) => ajax('getOtherInfo', { token }))
    .then(res => {
    // do something...
    })

 async/await的优势就在于处理then式调用链

posted @ 2021-03-26 10:14  我の前端日记  阅读(66)  评论(0)    收藏  举报
Copyright © 2021 LinCangHai
Powered by .NET 5.0 on Kubernetes