[英]nodejs windows exec promise pending
我正在運行exec
以從計算機硬件獲取 id。 我正在嘗試將 id 分配給變量 cpu_id,以便稍后在我的腳本中的 http 請求參數中使用它。 當控制台日志似乎總是輸出Promise { <pending> }
而不是捕獲的 id 時。
我玩過等待和異步,但無法讓事情按照他們應該的方式運行。 任何幫助或指示將不勝感激。
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 返回一個 Promise。 嘗試使用execSync
:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.