promise 如何返回值,而不是promise

function _getProductType (arr, num, str) {
  let type = ''
  arr.forEach(val => {
    if (num == val.code_val) { //eslint-disable-line
      type = val.code_desc
    } else if (num == '1000') { //eslint-disable-line
      type = '总计'
    }
  })
  return type + '' + (str || '')
}
export async function productType (num, str) {
  let productTypes = []
  function aaa () {
    return getByCode(52).then(res => {
      if (res.data.success) {
        productTypes = res.data.data
        return _getProductType(productTypes, num, str)
      }
    })
  }
  let b = await aaa()
  return b
}

productType 是vue的一个过滤器,但是返回的是promise,但是template中的label属性只接受string类型。
请问该如何处理这种情况(axios也是promise的...)

clipboard.png

阅读 6.8k
2 个回答

filters 只能是个同步函数。

export async function productType (num, str) {
  const res = await getByCode(52);
  if (res.data.success) {
    let productTypes = res.data.data;
    return _getProductType(productTypes, num, str)
  }
  return 'ERROR' //?
}

凭猜测写的,从代码上看你的aaa函数完全多余,不能运行的话理解下思路自己改

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题