vue中Promise和async await的实际运用

16 篇文章 0 订阅

原因是这样的。在vue文件中

当时想着很多页面公用一个方法,并且实行多元化使用。

在抽取方法的同时,在最后返回到页面上时,出现了console.log有值,但是页面上却没有的现象,所以判断出是同步异步的问题

API数据
import fetch from '@/utils/fetch'

// 获取Section
export function getSection(id,lcid) {
  return fetch({
    url: '/section/' + id + '?lcid='+ lcid,
    method: 'get'
  })
}
// 获取Section列表 (Section章节)
export function getSectionProductList(id,lcid) {
  return fetch({
    url: '/section/getProductList/' + id + '?lcid='+ lcid,
    method: 'get'
  })
}
JS方法

import {getSection, getSectionProductList} from '@/api/section'

// 获取section产品list
export async function sectionList(sectionId, lcid) {
// export function sectionList(sectionId, lcid) {
  let sectionList = {
    id: sectionId,
    title: '',
    subSectionList: []
  }

  // method one
  // return new Promise(resolve1 => {
  //   getSection(sectionId, lcid).then(response => {
  //     sectionList.title = response.data.result.title
  //     let subSectionList = response.data.result.subSectionList
  //     if (subSectionList == null || subSectionList.length === 0) {
  //       return
  //     }
  //     // 找子目录
  //     let tempData = subSectionList.map(list => {
  //       let obj = {}
  //       obj.id = list.id
  //       obj.title = list.title
  //       return new Promise(resolve => {
  //         getSectionProductList(list.id, lcid).then(response2 => {
  //           resolve(response2)
  //           obj.subSectionValue = response2.data.result
  //           sectionList.subSectionList.push(obj)
  //         })
  //       })
  //     })
  //     Promise.all(tempData).then(response => {
  //       resolve1(sectionList)
  //     }).catch(_ => {console.log('error')})
  //   })
  // })

  // method two
   let tempData = await getSection(sectionId, lcid)
    sectionList.title = tempData.data.result.title
  let subSectionList = tempData.data.result.subSectionList
  if (subSectionList == null || subSectionList.length === 0) {
    return
  }
  for (let i = 0; i < subSectionList.length; i++) {
    let obj = {}
    obj.id = subSectionList[i].id
    obj.title = subSectionList[i].title
    let tempList = await getSectionProductList(subSectionList[i].id, lcid)
    obj.subSectionValue = tempList.data.result
    sectionList.subSectionList.push(obj)
  }

  return sectionList
}
之后写出了2个方法,一个为Promise  一个为await 

但是第一种出来以后的数据可能是随机的,需要自己再去排序

下面为postman请求到的数据

getSection

getSectionProductList

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue使用async和await,可以通过在方法前面加上async关键字来定义异步函数,然后在需要等待的地方使用await关键字等待异步操作的结果。例如,可以在Vue组件的方法使用async和await来处理异步请求。 引用\[1\]的示例代码展示了在Vue使用async和await的基本用法。可以定义一个异步函数,使用await关键字等待axios.get方法返回的Promise对象的结果。然后可以在异步函数外部使用.then方法来处理异步操作的结果。 引用\[2\]提到,async/await是一种编写异步代码的新方法,它建立在Promise的基础上,让异步代码看起来更像同步代码。它的使用场景包括处理异步请求、处理定时器、处理文件读写等。 总结起来,在Vue使用async和await可以更方便地处理异步操作,使代码更加清晰易读。可以在Vue组件的方法使用async关键字定义异步函数,然后使用await关键字等待异步操作的结果。这样可以避免回调地狱,使代码更加简洁和易于维护。 #### 引用[.reference_title] - *1* [Vue接口调用(三)async/await用法](https://blog.csdn.net/m0_55990909/article/details/123981292)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue使用async和await(一文教会你)](https://blog.csdn.net/m0_52040370/article/details/124660219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vueasync和await的使用](https://blog.csdn.net/m0_58974838/article/details/117636647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值