1

I'm novice with javascript and I really don't know how can I get the "networks" value.

scanner.scan(function(error, networks){
   if(error) {
        console.error(error);
    } else {
        console.log(networks); << print the correct value
    }
});
console.log(networks) << print undefined

I just want to use "networks" outside function

Anyone could help me? Thanks!

6
  • is scan function asynchronous ? May 15, 2016 at 22:10
  • Your second console.log(networks) its out of the scope May 15, 2016 at 22:10
  • @dato, yeah, it is May 15, 2016 at 22:44
  • @YosvelQuintero, yep, I know that, but how can I use "networks" outside function? May 15, 2016 at 22:46
  • Reliably you cannot.
    – Wainage
    May 15, 2016 at 23:01

4 Answers 4

0

Add a variable before the scan function and set the value inside the function:

var netValue = null;
scanner.scan(function(error, networks){
   netValue = networks;
   if(error) {
      console.error(error);
    } else {
      console.log(networks); << print the correct value
    }
});
console.log(netValue);

The final line still may not work though if the scanner.scan() function is an asynchronous call.

0

You can create a global value networks, and update that from within the scan function:

var globalNetworks;

scanner.scan(function(error, networks){
    globalNetworks = networks;
    if(error) {
      console.error(error);
    } else {
      console.log(networks); << print the correct value
    }
});
console.log(networks) << print the correct value

I suggest that you read about how scope works in javascript: http://www.w3schools.com/js/js_scope.asp

0

The reason you cannot get the value of networks is because it is wrapped in a closure, the value is only available inside that function. Go here to read more about closures:

How do JavaScript closures work?

To have access to it outside you can create another variable outside of the function and set that to variable to network inside the function:

var otherNetworks;
scanner.scan(function(error, networks){
   otherNetworks = networks;
   if(error) {
      console.error(error);
    } else {
      console.log(networks); << print the correct value
    }
   });
 console.log(otherNetworks) << print correctValue
2
  • I already did it before, but it is only showing the global value, the variable is updated inside fuction, but does not persist its value outside. May 15, 2016 at 22:46
  • I highly doubt that, can you reproduce that in jsfiddle or jsbin?
    – omarjmh
    May 15, 2016 at 22:48
0

I already did it before, but it is only showing the global value:

var globalNetworks = "global"
var scanner = wifiscanner();

    scanner.scan(function(error, networks){
      globalNetworks = networks;
      console.log(globalNetworks) //displaying all the wlan0 networks available
      if(error) {
        console.error(error);
      } else {
        // console.log(networks);
      }
    });

    console.log(globalNetworks) // displaying global

btw, networks is a json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.