Nodejs-仅在存在错误的情况下才执行回调函数?

问题描述 投票:0回答:1

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

  });
}
javascript node.js function callback invoke
1个回答
0
投票

您可以尝试断开回调吗?

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

  });
});
  
}
© www.soinside.com 2019 - 2024. All rights reserved.