【发布时间】:2019-08-08 22:06:54
【问题描述】:
我正在尝试在我的 react/electron 项目中使用 async/await,但它不是 workink。 我想要的是获取 docker 容器状态列表。 但是 console.log(list) 返回 undefined。
async componentDidMount() {
let list = await this.getDockerList();
console.log(list);
}
async getDockerList() {
let files = fs.readdirSync('/home/Docker');
let dockerList = [];
let index = 0;
await files.forEach(async function (value, i) {
await exec('docker ps -a -q --filter=name=' + value + '_web_1 --filter=status=running', (err, stdout) => {
if (stdout !== '') {
dockerList[i] = { name: value, status: 1 };
} else {
dockerList[i] = { name: value, status: 0 };
}
if ((index + 1) === files.length) {
console.log('returned');
return dockerList;
}
index++;
});
});
}
有人可以帮帮我吗? :)
【问题讨论】:
-
我强烈建议使用 dockerode (npmjs.com/package/dockerode) 而不是直接运行命令,使用 exec 时需要考虑很多安全预防措施。甚至还有一个列出容器的例子github.com/apocas/dockerode/blob/master/examples/…
-
你可以更喜欢这个它可以为你提供充分的帮助 [stackoverflow.com/questions/44090197/]