javascript - return 关键字的行为很奇怪
问题描述
我正在开发一个需要检查 Windows 注册表中的键是否存在的 JS(Electron,Node.js)项目。这是我的代码:
function IsSettedUp() {
regedit.list('HKCU\\SOFTWARE')
.on('data', function (entry) {
//Returns Keys
console.log(entry.data.keys)
//Checks if "WinXSoft" appears in the entry.data.keys array
var key = $.inArray("WinXSoft", entry.data.keys)
console.log(key)
//Returns false if WinXSoft wasn't found
if (key == -1) {
return false
}
//Returns true if WinXSoft was found
else {
return true
}
})
}
//Should be true or false according to the WinXSoft key
var z = IsSettedUp();
console.log(z)
当我创建密钥并运行代码时,这是输出:
console.log(entry.data.keys)
输出Array(42)
。(预期)
console.log(key)
输出 13
(预期)
console.log(z)
输出undefined
(???,应该是真的)
所以,是的,你们知道我该如何解决这个问题吗?
解决方案
尝试从on
函数中返回值
function IsSettedUp() {
var result = false;
regedit.list('HKCU\\SOFTWARE').on('data', function (entry) {
//Returns Keys
console.log(entry.data.keys)
//Checks if "WinXSoft" appears in the entry.data.keys array
var key = $.inArray("WinXSoft", entry.data.keys)
console.log(key)
//Returns false if WinXSoft wasn't found
if (key == -1) {
result = true;
}
//Returns true if WinXSoft was found
else {
result = false;
}
});
return result;
}
推荐阅读
- c# - Unity 2D - 在两个(或更多)空间之间移动任何对象
- python - 我们如何替换列表中字符串中的引号?
- azure - Azure 自动化 - 我们能否将参数从 powershell 主运行手册传递给 python 子进程
- ruby-on-rails - 当在 Sidekiq 中引发错误时,如何丢弃在异步任务上执行的所有更改?
- docker - Docker 检查等效 w/buildkit/buildctl
- java - BridgeException: null:-1 将 SVG 转码为 BufferedImage 并引用不存在的掩码
- javascript - 凭据无效时向用户显示的错误消息
- google-sheets - Google 表格 - 计算给定日期之间 Importrange 数据的唯一值
- html - row-sm 和 row-cols-sm-n(其中 n<12) 有什么作用?我见过 col-sm-n 的用法(其中 n<12),但我不知道 row-sm 的用法
- apache-nifi - 使用 NiFi 将 json 内容放入字段中的 Jolt 转换