0

I'm having a javascript function which is returning me the ip address. The function is this:

const clientsIpAdress = (onNewIP) => {
  const MyPeerConnection =
    window.RTCPeerConnection ||
    window.mozRTCPeerConnection ||
    window.webkitRTCPeerConnection;
  const pc = new MyPeerConnection({
    iceServers: []
  });
  const noop = () => {};
  const localIPs = {};
  const ipRegex =
    /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;

  const iterateIP = (ip) => {
    if (!localIPs[ip]) onNewIP(ip);
    localIPs[ip] = true;
  };
  pc.createDataChannel('');
  pc.createOffer().then((sdp) => {
    sdp.sdp.split('\n').forEach((line) => {
      if (line.indexOf('candidate') < 0) return;
      line.match(ipRegex).forEach(iterateIP);
    });

    pc.setLocalDescription(sdp, noop, noop);
  });
  pc.onicecandidate = (ice) => {
    if (!ice || !ice.candidate ||
      !ice.candidate.candidate ||
      !ice.candidate.candidate.match(ipRegex)) return;
    ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
  };
};
export default clientsIpAdress;

When i import it, and write

const myIpAdress = clientsIpAdress((ip) => {
    console.log(ip);
  });

it logs me the correct ip. But when I'm writing this:

const myIpAdress = clientsIpAdress((ip) => {
        return(ip);
      });

and I do console.log(myIpAdress), undefined is being returned. Maybe, it's a foolish question but I got stuck. Thanks.

cello
  • 5,356
  • 3
  • 23
  • 28
RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • What you're trying to do isn't valid JS. Do just `return ip`. – glhrmv Jun 19 '17 at 10:13
  • 2
    @dogui `return(ip)` and `return ip` do the exact same thing. – Spencer Wieczorek Jun 19 '17 at 10:14
  • Really? Didn't know that. Thanks. – glhrmv Jun 19 '17 at 10:15
  • So, how to handle this? Sorry but i'm stuck – RamAlx Jun 19 '17 at 10:16
  • Read the duplicate; you're simply unfamiliar with asynchronous execution, like hundreds of other SO visitors every day. – deceze Jun 19 '17 at 10:17
  • I'm not making an asynchronous request. Just having a function which is producing something. Then, i import this function and i want to add what is returning to a variable – RamAlx Jun 19 '17 at 10:21
  • Your code *is* asynchronous. `createOffer` and `onicecandidate` will finish/fire *sometime later* → asynchronous. – deceze Jun 19 '17 at 10:34
  • Ok, const jkjk = clientsIpAdress((ip) => { return ip; }); it's like findItem(function(item) { // Do something with item }); as in the example. But it's also returnig undefined – RamAlx Jun 19 '17 at 10:35
  • Put a `console.log` right after wherever you call `clientsIpAddress`, and into the callback you pass to `clientsIpAddress`. You will notice that the log inside the callback happens *after* the log placed after `clientsIpAddress`. Because the callback is called asynchronously! If the callback happens *after* `clientsIpAddress` has already finished, then there's no way you can synchronously *return* a value from it! – deceze Jun 19 '17 at 10:55
  • A now i see what you are talking about? Therefore, i'm a little bit confused on how to return the value of the clientsIpAddress – RamAlx Jun 19 '17 at 11:02
  • Read. The. Duplicate. – deceze Jun 19 '17 at 11:10
  • I can't find a solution.... – RamAlx Jun 19 '17 at 11:47

0 Answers0