0

when I'm trying to pass the values to an array and return them the console only shows an empty array or say undefined!
my problem is .then don't pass the value to the array

const net = require('net');
const find = require('local-devices');
class Network{
  hostname : string = "";
  port = 80 ;
  deviceList: any = [];

  
  public connect(): void{
      if (net.isIPv4(this.hostname)){
          var connection = net.createConnection({port : this.port, host : this.hostname});
          console.log(connection);
      }
  }
  public findDevices(){
    var boom:object[] = [];
    find().then((devices:any[])=>{
      this.deviceList.push(devices);
    })
    return this.deviceList;
  }

}
const monitor = new Network();
let data = monitor.findDevices();
console.log(data);

Dave
  • 3
  • 1
  • 4
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ivar Aug 18 '22 at 09:38

2 Answers2

0

Add async await inside findDevices

  async public findDevices(){
    var boom:object[] = [];
    var devices = await find()
    this.deviceList = devices;
    return this.deviceList;
  }

your existing code is not stopping at find unitil it brings the devices, so its running over before it brings the devices so on adding async await , it will stop until it brings devices

once class is updated with async update then update below code wile executing in end

const monitor = new Network();
monitor.findDevices();
console.log(monitor.deviceList);
vijay
  • 10,276
  • 11
  • 64
  • 79
  • 1
    Not claiming the downvote here, but please see [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) (Specifically the "_Answer well-asked questions_" part.) Besides that, the answer is plain wrong. Calling `monitor.findDevices()` without `await` means that `console.log(monitor.deviceList)` is evaluated _before_ the promise has been resolved. – Ivar Aug 18 '22 at 09:52
  • 1
    it doesn't work and returns "Promise { }" – Dave Aug 18 '22 at 09:57
  • @Dave is right it can't work, the caller didn't wait for the promise to resolve. The function is marked as async but the upper thread won't wait for it to resolve. The await will only be effective in the findDevices function, but the console.log is called immediately after it (and will probably not be resolved either) – nook Aug 18 '22 at 10:01
0

I think the reason it's undefined or empty, it's because the function returns earlier than the promise resolves. I made a few edits so the values are correctly resolved, and wrapped it in an async/await function.

const net = require('net');
const find = require('local-devices');

class Network {
  hostname: string = "";
  port = 80;
  deviceList: any = [];


  public connect(): void {
    if (net.isIPv4(this.hostname)) {
      var connection = net.createConnection({ port: this.port, host: this.hostname });
      console.log(connection);
    }
  }
  public findDevices() {
    var boom: object[] = [];
    // Return the promise, so the caller can "await" it
    return find().then((devices: any[]) => {
      // Spread devices, otherwise it will push the array instead of the values
      this.deviceList.push(...devices);
      // Promise return value
      return this.deviceList
    })
  }

}

async function run() {
  const monitor = new Network();
  let data = await monitor.findDevices();
  console.log(data)
}

run()

If you don't want to have the "run" method you can still do as follows:

const monitor = new Network();
monitor.findDevices()
  .then((deviceList) => {
    // This is the return value of the promise
    console.log(deviceList)
    // This is the property deviceList of Network
    console.log(monitor.deviceList)

    // They are both the same
  });
nook
  • 1,729
  • 1
  • 12
  • 34