要异步读取的变量的值
Value of a variable to be read asynchronously
我从注册表中读取值并将注册表中的值设置为代码中的变量。我看到从注册表中获取值是异步发生的。
我是来自 node 的 regedit 包。我试过用 setTimeout
代替 constructENVTable
,但没用。
请从 html 页面主体 onload
触发 note:mainfunc()
//declared globally
var regValue = "";
function mainfunc() {
setRegistryValueForFirstTime();
constructENVTable();
}
function setRegistryValueForFirstTime() {
let path = "HKLM\....\Environment";
regedit.list('HKLM\...\Environment', function(err, result) {
try {
regValue = result[path].values.XYZ.value;
alert(regValue); //prints value correctly
app.console.log("Registry value for ANSYS_LI is already set");
} catch (err) {
app.console.log(err);
app.console.log("setting up registry value");
setRegistryValue();
}
alert(regValue); //doesn't print value
});
}
function constructENVTable() {
alert(regValue); //doesn't print value
}
为了简单起见,你可以运行函数一取值,然后在第二个函数中传递。
var regValue = "";
function mainfunc() {
setRegistryValueForFirstTime();
}
function setRegistryValueForFirstTime() {
let path = "HKLM\....\Environment";
regedit.list('HKLM\...\Environment', function(err, result) {
try {
regValue = result[path].values.XYZ.value;
app.console.log("Registry value for ANSYS_LI is already set");
constructENVTable(regValue); //prints value correctly
} catch (err) {
app.console.log(err);
app.console.log("setting up registry value");
setRegistryValue();
}
});
}
function constructENVTable(regValue) {
alert(regValue); // get value here and alert it
}
您可以用 Promise 包装 regValue
:
var regValue;
function mainfunc() {
setRegistryValueForFirstTime();
constructENVTable();
}
function setRegistryValueForFirstTime() {
let path = "HKLM\....\Environment";
regedit.list('HKLM\...\Environment', function (err, result) {
try {
regValue = new Promise(
(resolve, reject) => resolve(result[path].values.XYZ.value)
)
app.console.log("Registry value for ANSYS_LI is already set");
} catch (err) {
app.console.log(err);
app.console.log("setting up registry value");
setRegistryValue();
}
alert(regValue); //doesn't print value
});
}
function constructENVTable() {
// alert(regValue); //doesn't print value
regValue.then(
value => alert(value)
)
}
我从注册表中读取值并将注册表中的值设置为代码中的变量。我看到从注册表中获取值是异步发生的。
我是来自 node 的 regedit 包。我试过用 setTimeout
代替 constructENVTable
,但没用。
请从 html 页面主体 onload
//declared globally
var regValue = "";
function mainfunc() {
setRegistryValueForFirstTime();
constructENVTable();
}
function setRegistryValueForFirstTime() {
let path = "HKLM\....\Environment";
regedit.list('HKLM\...\Environment', function(err, result) {
try {
regValue = result[path].values.XYZ.value;
alert(regValue); //prints value correctly
app.console.log("Registry value for ANSYS_LI is already set");
} catch (err) {
app.console.log(err);
app.console.log("setting up registry value");
setRegistryValue();
}
alert(regValue); //doesn't print value
});
}
function constructENVTable() {
alert(regValue); //doesn't print value
}
为了简单起见,你可以运行函数一取值,然后在第二个函数中传递。
var regValue = "";
function mainfunc() {
setRegistryValueForFirstTime();
}
function setRegistryValueForFirstTime() {
let path = "HKLM\....\Environment";
regedit.list('HKLM\...\Environment', function(err, result) {
try {
regValue = result[path].values.XYZ.value;
app.console.log("Registry value for ANSYS_LI is already set");
constructENVTable(regValue); //prints value correctly
} catch (err) {
app.console.log(err);
app.console.log("setting up registry value");
setRegistryValue();
}
});
}
function constructENVTable(regValue) {
alert(regValue); // get value here and alert it
}
您可以用 Promise 包装 regValue
:
var regValue;
function mainfunc() {
setRegistryValueForFirstTime();
constructENVTable();
}
function setRegistryValueForFirstTime() {
let path = "HKLM\....\Environment";
regedit.list('HKLM\...\Environment', function (err, result) {
try {
regValue = new Promise(
(resolve, reject) => resolve(result[path].values.XYZ.value)
)
app.console.log("Registry value for ANSYS_LI is already set");
} catch (err) {
app.console.log(err);
app.console.log("setting up registry value");
setRegistryValue();
}
alert(regValue); //doesn't print value
});
}
function constructENVTable() {
// alert(regValue); //doesn't print value
regValue.then(
value => alert(value)
)
}