我需要通过参数来解决新的承诺。我需要传递的数据来自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'))
}
})
})
当我运行this时,我在控制台中拥有的就是这个
Promise {<pending>}
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: undefined
有什么想法吗?
答案 0 :(得分:0)
也许在这段代码中:
ngjs.on('browse', () => {
return new Promise((resolve, reject) => {
try {
const value = uploadFile()
resolve(value)
} catch(e) {
reject(new Error('something failed'))
}
})
})
尝试在uploadFile()之前添加“等待”,即:
const value = await uploadFile()
答案 1 :(得分:0)
在使用浏览功能的地方,请先使用await。当我们忘记等待时,会产生承诺{pending} 问题。