1

Yes, I've tried other solutions I have found but to no avail. I've tried setting a variable (region) above the promise but realized that it'd be out of the scope so this was my next solution. Whenever I try to return region it comes back as undefined. Whenever I console.log it (in the same place) it works just fine. Any insight to this?

EDIT: iplocation is an npm package iplocation

function getRegion(ipAddr) {
    iplocation(ipAddr)
        .then(res => {
            if (res.country == 'US') {
                region = res.country + "-" + states[res.region_code];
            } else {
                region = res.country;
            }
            return region;
        })
}
Chris
  • 13
  • 3

4 Answers4

0

Promises run asynchronously. That means by the time the .then part is run, the outer function has returned undefined. You could make the outer function async and then await the value of the Promise.

Max
  • 1,325
  • 9
  • 20
  • So async the getRegion function then await the iplocation promise? If I do that I just get `Promise { }` – Chris Jul 21 '18 at 23:53
0
async function getRegion (ipAddr) {
  let res
  try {
    res = await iplocation(ipAddr)
    if (res.country === 'US') {
      return `${res.country} - ${states[res.region_code]}`
    } else {
      return res.country
    }
  } catch (err) {
    throw err
  }
}

Usage:

getRegion('8.8.8.8')
  .then(loc => console.log(loc))
  .catch(err => console.log(err))
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • So basically I need to take what "loc" would equal and assign it to something in an object preferably outside the promise. – Chris Jul 22 '18 at 00:07
  • Nevermind, I added the object to the promise and got it to work. Thanks! – Chris Jul 22 '18 at 00:10
  • There's really no need for `await` here and you've "eaten" the error and are returning `err` as if it was a country. This is not proper error handling. – jfriend00 Jul 22 '18 at 00:11
  • What you are missing is the `async` nature of `node.js`. Code would not be executed from top to down. It could be executed in any order depending on the code. That's the big difference from languages like C. – Xaqron Jul 22 '18 at 00:13
  • Why do you even have the `try/catch` when all you're doing in the catch is `throw err`? Code would be simpler without it. The `async` function will catch any exception of rejected promise and propagate it to the caller as a rejected promise. You don't have to do that manually. – jfriend00 Jul 22 '18 at 03:23
  • @jfriend00: For the sake of learning. – Xaqron Jul 22 '18 at 10:14
0

Your operation is asynchronous so you need to return a promise and the caller needs to use .then() with it:

function getRegion(ipAddr) {
    // return the promise
    return iplocation(ipAddr).then(res => {
        // return value will be resolved value of the promise
        if (res.country == 'US') {
            return res.country + "-" + states[res.region_code];
        } else {
            return res.country;
        }
    });
}

getRegion(...).then(region => {
  // use the region in here
  console.log(region);
}).catch(err => {
  // handle error here
  console.log(err):
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
-1

You need to return the promise you are awaiting. You could use return new Promise(...) or use async/await as seen below.

async function getRegion(ipAddr) {
   const res = await iplocation(ipAddr)
   if (res.country == 'US') {
     return res.country + "-" + states[res.region_code];
   }
   return res.country;
}
Alan Friedman
  • 1,582
  • 9
  • 7