Search code examples
javascriptasynchronouswait

Work with data that I get out of an async function


I am doing an asynchronous network search, that finds the ip adresses of all devices in the local network.

Now I want to find one specific ip that matches a mac adress out of the found devices.

My problem is, since the search is asynchronous, I cant just take the output, save it as an array into a const, and search inside of it for the specific mac adress. It always gives me the "promise undefined" output.

To make it clear, here is my code:

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

async function findIP() {
  // Find all local network devices.
  const found = find().then(devices => {
    console.log(devices);
  })

  await found;

  // here I want to work with the data thats saved inside found and find the ip adress
  console.log(found);
}

findIP();

the output of this is:

    [
  { name: '?', ip: '192.168.0.43', mac: '2a:f2:8c:83:26:8a' },
  { name: '?', ip: '192.168.0.91', mac: '98:da:c4:ff:1c:e1' },
  { name: '?', ip: '192.168.0.152', mac: 'dc:a6:32:01:aa:cc' },
  { name: '?', ip: '192.168.0.175', mac: '00:17:88:65:f4:2d' },
  { name: '?', ip: '192.168.0.182', mac: '4e:98:8d:e5:05:51' },
  { name: '?', ip: '192.168.0.211', mac: '80:be:05:73:bc:g5' }
    ]
Promise { undefined }

So, what I actually want to do with the "found" array is something like:

  const element = found.find(d => d.mac === '2a:f2:8c:83:26:8a');
  console.log(element ? element.ip : '');

So that I get the Ip for one specific mac adress. But if I put this in my code, (instead of the console.log(found), I get an "UnhandledPromiseRejectionWarning: TypeError: found.find is not a function"

So where do I have to put this? I found some functions that wait a specific amount of time like this one:

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: 'resolved'
}

asyncCall();

But I dont know where to put this function inside my code, so that the syntax is correct and it waits long enough, that the promise is not pending anymore.

So basically I just want to tell my program: start the network search, wait 5 seconds, then save all found devices in a const xy. After that do console.log(xy)

Is it even possible what I am trying to do?


Solution

  • You need to return something from your promise and you need to assign that returned value somewhere.

    const find = require('local-devices');
    
    async function findIP() {
      // Find all local network devices.
      const found = find().then(devices => {
        return devices;
      });
      // you could skip the .then(...) in the above example
      // if you only return what is passed to the .then
    
      const foundDevices = await found;
    
      // here I want to work with the data thats saved inside found and find the ip adress
      console.log(foundDevices);
    }
    
    findIP();
    

    For your specific requirements you could skip the .then completely and just const found = await find();

    const find = require('local-devices');
    
    async function findIP() {
      const found = await find();
      const element = found.find(d => d.mac === '2a:f2:8c:83:26:8a');
      console.log(element ? element.ip : '');
    }
    
    findIP();