首页 > 解决方案 > 从外部的 axios 返回响应值

问题描述

我需要通过参数来解决新的承诺。我需要传递的数据来自 axios 的响应,这是我的 axios 代码

async function uploadFile() {
const {value: file} = await Swal({
      title: 'Select image',
      input: 'file',
      inputAttributes: {
        'accept': 'image/*',
        'aria-label': 'Upload your profile picture'
      }
    })

    if (file) {
      const reader = new FileReader
      reader.onload = (e) => {
        Swal({
            title: 'Your uploaded picture, Do you want to upload it?',
              text: "Ypur photo will be uploaded!",
              type: 'info',
              imageUrl: e.target.result,
              showCancelButton: true,
              confirmButtonColor: '#3085d6',
              cancelButtonColor: '#d33',
              confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if(result.value) {
                console.log(file, 'file')
                let formData = new FormData;
                formData.append('photo', file);
                let settings = { headers: { 'Content-Type': 'multipart/form-data'}}
                axios.post('/agent_dashboard/save_photo', formData)
                .then((res) => {
                    const vl = res.directory => this i want to use
                    return vl
                })
                .catch((err) => {
                    console.log(err.toString(), 'error')
                })
            }
        })
      }
      reader.readAsDataURL(file)
    }
}

这是我的另一个需要 axios 响应对象的函数

      ngjs.on('browse', () => {
         return new Promise((resolve, reject) => {
          try   {
             const value = uploadFile()

            resolve(value)
        } catch(e) {
            reject(new Error('something failed'))
        }
    })

  })

当我运行这个时,我在控制台中只有这个

   Promise {<pending>}
   [[PromiseStatus]]: "resolved"
   [[PromiseValue]]: undefined

有任何想法吗?

标签: javascriptpromiseaxios

解决方案


在您使用浏览功能的地方,在它之前使用等待。当我们忘记等待时,会产生Promise {pending}问题。


推荐阅读