简体   繁体   English

使用 async/await 将 promise 的结果保存在数组中

[英]Save result of promises inside an array with async/await

I'm writing a script that should count elements in a JSON file.我正在编写一个脚本,它应该计算 JSON 文件中的元素。 I can't figure out how to save results of promises in an array.我不知道如何将承诺的结果保存在数组中。

This is the async function that counts the elements:这是计算元素的异步 function:

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
        })
}

The function that calls it is this:调用它的 function 是这样的:

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')
    }
}

How can I push results in the count array?如何将结果推送到count数组中? At the moment, it's returning [] .目前,它正在返回[]

Many thanks非常感谢

async function countVulnerabilities(dir, project, branch) {
    return await count[dir](new URL(path.join('/dropbox/sast/', project, branch, dir), process.env.REMOTE_DRIVE_PATH))
}

what is count in this function?这个 function 的计数是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM