0
async function getDNShomeIP(){
    var response = await fetch('https://dns.google/resolve?name=example.com'); // this uses the google api
    var json = await response.json();
    var homeIPadress = json.Answer[0].data;
    console.log(homeIPadress);
    return homeIPadress;
};

async function getCurrentIP(){
  var response = await fetch("https://api.ipify.org?format=json");
  var json = await response.json()
  var currentIPadress = json.ip;
  return currentIPadress;
}

var homeIPadress = getDNShomeIP();
var currentIPadress = getCurrentIP();

if (homeIPadress == currentIPadress){
  alert("from same from lol");
} else {
  alert("not from same")  
};

Hi there,

I wanted to know how to compare the values of two promises in Javascript. I can't work out how to make the program wait before the if statement. The statement just evaluates to false if the promises are not yet fulfilled so program follows the else branch.

Thanks

4 Answers4

2

Use the "await" keyword inside another "async" function so that the function waits for a response before it continues execution.

async function testEquality() {
  var homeIPadress = await getDNShomeIP();
  var currentIPadress = await getCurrentIP();

  if (homeIPadress === currentIPadress) {
    alert("from same from lol");
  } else {
    alert("not from same")
  };
}

testEquality();

I would also recommend you use triple equal (===) to compare the results as this uses strict equality comparison.

Lahiru Tennakoon
  • 621
  • 1
  • 6
  • 8
1

You could wrap it in another async function:

async function execute() {
  var homeIPadress = await getDNShomeIP();
  var currentIPadress = await getCurrentIP();

  if (homeIPadress == currentIPadress){
    alert("from same from lol");
  } else {
    alert("not from same");
  }
}

execute();

1

first you need to fix your fetch function, then fix the async await function
you have to put your await function on async function just like this, fix your getdnshomeip just like the code below

function getCurrentIP(){
   return fetch("https://api.ipify.org?format=json")
    .then( r => r.json())
    .then( r => r);
}

const check = async () => {
   var currentIPadress = await getCurrentIP();
   var homeIPadress = await getDNShomeIP();

   if (homeIPadress === currentIPadress){
      alert("from same from lol");
   } else {
      alert("not from same")
   };

}

check();
1

You're describing a function I created

async function getDNShomeIP(){
    var response = await fetch('https://dns.google/resolve?name=example.com');
    var json = await response.json();
    var homeIPadress = json.Answer[0].data;
    console.log(homeIPadress);
    return homeIPadress;
};

async function getCurrentIP(){
  var response = await fetch("https://api.ipify.org?format=json");
  var json = await response.json()
  var currentIPadress = json.ip;
  console.log(currentIPadress);
  return currentIPadress;
}

const { eq } = rubico

eq(getDNShomeIP, getCurrentIP)().then(console.log)
<script src="https://unpkg.com/rubico/index.js"></script>

documentation for eq

richytong
  • 2,387
  • 1
  • 10
  • 21
  • Thanks, I didn't realise that that existed already I had just used other bits from stack overflow and made the code itself. Random that we ended up at the same conclusion. lol – Chaim Stanton Jun 05 '20 at 04:24
  • Yeah, I'd say it's quite a lucky circumstance :-) – richytong Jun 05 '20 at 04:26