javascript - 从外部的 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
有任何想法吗?
解决方案
在您使用浏览功能的地方,在它之前使用等待。当我们忘记等待时,会产生Promise {pending}问题。
推荐阅读
- reactjs - ASP.Net Core 3 + React Web App - 看到资源繁忙错误
- python-3.x - 如何使用 Pandas txt 文件读取每行中的列名
- delphi - 按频率排序 Delphi TStringList
- php - VS Code - 使 Intellisense “阅读更多”(建议详细信息)框更大或文本更小
- database - 如何在 PostgreSQL 表中使用自定义函数作为默认列
- firebase - 如何让 Cloud Tasks 只运行一次?
- ansible - 无法连接到领事代理,错误是 HTTPConnectionPool
- laravel - Laravel,Vue Js 与 Vuetify 和 Vuelidate
- android - Android导航组件未显示正确的操作栏标题
- c# - EntityFramework核心错误“值不在预期范围内”