【发布时间】:2022-01-11 15:32:58
【问题描述】:
我正在编写一个脚本来计算 JSON 文件中的元素。我不知道如何将 Promise 的结果保存在数组中。
这是计算元素的异步函数:
async function countVulnerabilities(dir, project, branch) {
await count[dir](new URL(path.join('/dropbox/sast/', project, branch, dir), process.env.REMOTE_DRIVE_PATH))
.then((result) => {
return result
})
}
调用它的函数是这样的:
export function getLogs(req, res, id, project, branch) {
let count = []
if (checkMappedDrive()) {
getDirs(project, branch)
.then((dirs) => {
dirs.forEach((dir) => {
countVulnerabilities(dir, project, branch)
.then((result) => {
count.push(result)
console.log(result)
})
})
})
res.send(count)
} else {
res.send('Impossible to enstablish a connection with the remote drive')
}
}
如何将结果推送到count 数组中?目前,它正在返回[]。
非常感谢
【问题讨论】:
-
混合使用
async和.then(...)似乎是一种代码味道。 -
我可以返回响应,我无法将它保存在数组中...
标签: javascript node.js express asynchronous async-await