我正在尝试编写一个使用此节点库(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");
});
});
}