【问题标题】:Save result of promises inside an array with async/await使用 async/await 将 Promise 的结果保存在数组中
【发布时间】: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 数组中?目前,它正在返回[]

非常感谢

【问题讨论】:

标签: javascript node.js express asynchronous async-await


【解决方案1】:
async function countVulnerabilities(dir, project, branch) {
    return await count[dir](new URL(path.join('/dropbox/sast/', project, branch, dir), process.env.REMOTE_DRIVE_PATH))
}

这个函数的计数是什么?

【讨论】:

  • 另一个计算对象中元素的函数。但一切都在那里工作
  • 但你一开始什么都算不上
  • ok,告诉我这个函数在做什么或者你想让它做什么
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 2021-05-13
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 2022-09-30
  • 1970-01-01
相关资源
最近更新 更多