Javascript nodejs windows执行承诺挂起

Javascript nodejs windows执行承诺挂起,javascript,node.js,promise,exec,Javascript,Node.js,Promise,Exec,我正在运行exec从计算机硬件获取id。我试图将id分配给变量cpu_id,以便稍后在http请求参数的脚本中使用它。当控制台日志似乎总是输出Promise{}而不是捕获的id时 我曾经尝试过wait和async,但无法让事情按应有的方式运行。任何帮助或指点都将不胜感激 function get_cpu_id() { if (process.platform === "win32") { return execShellCommand('wmic csproduct ge

我正在运行
exec
从计算机硬件获取id。我试图将id分配给变量cpu_id,以便稍后在http请求参数的脚本中使用它。当控制台日志似乎总是输出
Promise{}
而不是捕获的id时

我曾经尝试过wait和async,但无法让事情按应有的方式运行。任何帮助或指点都将不胜感激

function get_cpu_id() {
    if (process.platform === "win32") {
        return execShellCommand('wmic csproduct get UUID /format:list').then(function(std){
            return std.replace(/\s/g, '').split("=")[1];
        });
    } else {
        return execShellCommand('cat /proc/cpuinfo | grep Serial').then(function(std){
            return std;
        });
    }
}

function execShellCommand(cmd) {
    const exec = require('child_process').exec;

    return new Promise((resolve, reject) => {
        exec(cmd, (error, stdout, stderr) => {
            if (error) {
                console.warn(error);
            }

            resolve(stdout ? stdout : stderr);
        });
    });
}

let cpu_id = get_cpu_id();

console.log(cpu_id);
Exec返回一个承诺。 尝试使用:


get\u cpu\u id
返回一个承诺。。。因此,
cpu\u id
是一个承诺。。。尝试
get\u-cpu\u-id()。然后(cpu\u-id=>console.log(cpu\u-id))
。然后(函数(std){return std;})是多余的“不做任何事”代码,您可以将其删除。如果我需要更改unix设备的串行id,我会在那里进行分析。@JaromandaX但是我如何将该值分配给变量cpu_id,以便以后在ajax请求脚本中使用它呢?为此,您需要学习如何使用异步代码-对于相同(不可能)要求的堆栈溢出有很多问题-所以做一些研究-因为您发布的代码的解决方案是我建议的-但是您想要使用的代码是不同的-所以,您需要做一些研究
const execSync = require('child_process').execSync;

function get_cpu_id() {
    if (process.platform === "win32") {
        return execSync('wmic csproduct get UUID /format:list').toString();
    } else {
        return execSync('cat /proc/cpuinfo | grep Serial').toString();
    }
}


let cpu_id = get_cpu_id();

console.log(cpu_id);