es6函数的.then中无法return结果

export function postMethod(data) {
  const baseURL = process.env.BASE_API
  axios({
    method: 'post',
    url: baseURL + '/img_upload',
    timeout: 50000,
    data: data,
    headers: {
      'loginToken': getLoginToken()
    }
  }).then(res => {
    const respose = res.data
    if (respose.errno == 'success') {  //成功
      //console.log(respose.data)
      //******************此处返回((((((((((((((((
      return respose.data
      //******************此处返回))))))))))))))))
    } else if (respose.errno == 'fail') {  //失败
      Message({
        message: respose.msg,
        type: 'error',
        duration: 2 * 1000
      })
    } else { //登录失败
      Message({
        message: '登录失败',
        type: 'error',
        duration: 2 * 1000
      })
    }
  })
}

在别处使用postMethod(data)时,结果是undefined。
这个return没起作用,如何让函数返回调取接口时返回的数据?

阅读 6.3k
3 个回答
return axios....
postMethod(data).then(resposeData=>{
    ...
})
export function postMethod(data) {
  const baseURL = process.env.BASE_API
  return axios(...)
export async function postMethod (data) {
    ...
    return await axios({
        ...
    }) 
}
async funcion getRes (...) { // 操作返回的数据
    let res = await postMethod(...)
    ..... // 操作res
}
// 调用
getRes(...)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题