【发布时间】:2023-02-09 14:39:11
【问题描述】:
const processInboundEmailAttachment = async (files: any[]): Promise<any[]> => {
const attachments = []
await Promise.all(
files.map(async (file) => {
try {
let response = null
response = await axios.get(file.url, {
headers: {
Accept: "message/rfc2822"
},
auth: {
username: "api",
password: "api-key"
},
responseType: "stream"
})
if (response && response.data) {
response.data.pipe(concat(async data => {
try {
const mediaUrl = await uploadToFirestore(file["content-type"], data, file.name)
attachments.push({
name: file.name,
url: mediaUrl,
id : uuidv4()
})
} catch (error) {
console.log("err", error);
}
}));
}
} catch (err) {
log.error(err)
}
})
)
return attachments // from here this return initial attachments [] array
}
const uploadAttachment = async () => {
const attachment = await processInboundEmailAttachment(JSON.parse(attach))
console.log("attachment",attachment);
// I want updated pushed attachment array here but I got [] initial decalare value
}
app.get("/uploadAttachment", uploadAttachment)
在附件控制台日志中我得到了 [] 数组,它返回数组的初始赋值。 它不是等待 API 响应和新推送的数组。
我认为 Promise 中存在一些问题,不是等待更新数组,而是 直接返回初始附件数组
提前感谢您的帮助
【问题讨论】:
-
实际产量:-
[ ]预期产出:-[{name,url,id}{name,url,id}....] -
什么是
concat?
标签: javascript node.js asynchronous async-await es6-promise