javascript - Nodejs - 回调函数仅在存在错误时执行?
问题描述
我正在尝试使用此节点库(https://www.npmjs.com/package/node-wifi)编写一个函数以连接到 wifi 网络,但似乎每当我执行此函数时,除非实际上是错误目前,回调被完全跳过。我可能误解了回调的工作方式,但即使没有检测到错误,它也不应该执行吗?
function ConnectToNetwork(networkName: string, networkPassword: string) {
// Connect to a network
wifi.disconnect();
wifi.connect({ ssid: networkName, password: networkPassword }, function (err) {
//This is not logged when no error is present
console.log("callback?");
if (err) {
console.log(err);
console.log("Couldn't connect to network");
return createFailureWindow();
}
//Shouldn't this part of the function be invoked if there is no error?
UpdateCurrentConnections();
createSuccessWindow();
console.log("Connected");
});
}
解决方案
您可以尝试断开回调吗?
function ConnectToNetwork(networkName: string, networkPassword: string) {
// Connect to a network
wifi.disconnect(function(err) {
if (err) {
console.log(err);
}
console.log("Disconnected");
wifi.connect({ ssid: networkName, password: networkPassword }, function (err) {
//This is not logged when no error is present
console.log("callback?");
if (err) {
console.log(err);
console.log("Couldn't connect to network");
return createFailureWindow();
}
//Shouldn't this part of the function be invoked if there is no error?
UpdateCurrentConnections();
createSuccessWindow();
console.log("Connected");
});
});
}
推荐阅读
- sql - SQL,如何通过将行转换为列来使用表创建视图
- php - WordPress 或 Drupal 用于构建一个像数据库但流量低的网站?
- javascript - Vue - 仅使用在道具中传递的变量来限制子组件
- android - 如何在android中用3种颜色和141度制作渐变
- javascript - array[index] === undefined 是否总是与 typeof array[index] === 'undefined' 相同?
- c# - 单元测试 IServiceCollection 注册
- javascript - 如何在javascript中替换字符串中的字符串值
- string - 需要将字符串放在文件中的特定位置
- sql - 选择字符串的特定部分
- javascript - Jquery:迭代表。问题=“无法读取未定义的属性'0'”