简体   繁体   English

Nodejs - 回调 function 仅在存在错误时执行?

[英]Nodejs - Callback function only executing if error exists?

I'm trying to write a function to connect to a wifi.network using this node library ( https://www.npmjs.com/package/node-wifi ) but it seems that whenever I execute this function, unless an error is actually present, the callback is skipped entirely.我正在尝试使用此节点库( https://www.npmjs.com/package/node-wifi )编写一个 function 以连接到 wifi.network,但似乎每当我执行此 function 时,除非出现错误实际上存在,回调被完全跳过。 I might be misinterpreting how callbacks work, but shouldn't it execute even in the event that there is no error detected?我可能误解了回调的工作原理,但即使没有检测到错误,它也不应该执行吗?

 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"); }); }

Can you try in disconnect callback?您可以尝试断开连接回调吗?

 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"); }); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM